[PyQt] Qt/PyQt 5.9.x. Correct way to get an "enumeration flags" to return 0?

J Barchan jnbarchan at gmail.com
Thu Jul 11 16:32:27 BST 2019


On Thu, 11 Jul 2019 at 16:07, Phil Thompson <phil at riverbankcomputing.com>
wrote:

> On 11/07/2019 15:59, J Barchan wrote:
> > On Thu, 11 Jul 2019 at 15:10, Phil Thompson
> > <phil at riverbankcomputing.com>
> > wrote:
> >
> >> On 11/07/2019 07:56, J Barchan wrote:
> >> > On Wed, 10 Jul 2019 at 21:28, Barry Scott <barry at barrys-emacs.org>
> >> > wrote:
> >> >
> >> >>
> >> >>
> >> >> On 10 Jul 2019, at 11:55, J Barchan <jnbarchan at gmail.com> wrote:
> >> >>
> >> >> If I return QOrientations() that works, but I'm unsure if that
> >> >> guarantees
> >> >> to return zero.
> >> >>
> >> >>
> >> >> I've not tried this - not ready to update Qt yet - does this print 0
> >> >> to
> >> >> confirm the value in the enum?
> >> >>
> >> >> print( int( QOrientations())
> >> >>
> >> >> Barry
> >> >>
> >> >>
> >> > I confirm that int( QOrientations() ) returns 0.  Assuming that is
> >> > consistent/deliberate and not random (which I imagine it is) I guess
> >> > that
> >> > is what I will use here.
> >> >
> >> > I am still a little troubled by the fact that I can convert enum to
> int
> >> > via
> >> > int(QOrientations(enum_value)) but have trouble converting int to enum
> >> > via
> >> > QOrientations(int_value).  However the latter does seem to be a
> PyCharm
> >> > IDE
> >> > warning ("Unexpected type 'int', expected 'Orientations',
> >> > 'Orientation'")
> >> > but works at run-time, so I guess it's OK even if I cannot find a
> >> > formula
> >> > which works without warning.  It would be nice to see a PyQt expert
> >> > statement on what is the correct way to handle this not-unusual
> >> > requirement....
> >>
> >> As you say the warning is coming from your IDE and I have no idea what
> >> introspection it is doing to determine exactly what types are allowed.
> >> As Orientation is an enum then an int is also allowed.
> >>
> >> Phil
> >>
> >
> > Hi Phil,
> >
> > You wrote:
> >
> > As Orientation is an enum then an int is also allowed.
> >
> >
> >  Yes, Orientation is an enum, so Orientation(0) is an acceptable
> > construct
> > for that.  But the function returns an Orientations, not an
> > Orientation.
> > Note the spelling (the extra "s" on the end), it's vital.  Orientations
> > is
> > a *flags*, not an *enum*.  The IDE accepts Orientation(0) (an enum) but
> > not
> > Orientations(0) (a flags).  For Orientations it only accepts one or
> > more
> > Orientation joined together.
> >
> > That is why in my latest post a while ago, which you don't seem to be
> > replying to, I said I seemed to have cracked it: Orientations(0)
> > offends
> > the IDE, but Orientations(Orientation(0)) works good!
>
> Yes, I know. As we both said, that warning is coming from the IDE.
> Orientations(0) is fine as far as PyQt is concerned (because passing
> Orientation is Ok and, therefore, passing 0 is Ok).
>
> Phil
>

Hi Phill,

Yes, I see, thanks.

It's a touch confusing (to me).  I have an answer which works for me now,
so the following is an academic question not a show-stopper.

You made the point of saying:

because passing Orientation is Ok and, therefore, passing 0 is Ok


which implies that 0 is a valid Orientation.  The C++ code for the function
returning an Orientations just goes return 0.  When I tried that first from
Python/PyQt I got a *run-time* error (so nothing to do with IDE) stating
something like "0 is an int and cannot be converted/used as a
Orientations".  I guess this is because at some level the C++ enum stuff is
just treated as an int, but in Python/PyQt Orientation/Orientations are
implemented as some kind of *class*, and that's different enough to cause
the run-time failure, is that the reason?

-- 
Kindest,
Jonathan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190711/08bcb03c/attachment.html>


More information about the PyQt mailing list