[PyQt] Checking a window's state using '&' is always returning False

Jayson Kempinger jkempinger at decisionvis.com
Mon Aug 21 21:55:22 BST 2017


How should I be checking the window state then?  The same comparison works well for Qt alignment and Qt modifier keys:

> if event.modifiers() & Qt.ShiftModifier:
>     shift = True
> if event.modifiers() & Qt.ControlModifier:
>     ctrl = True

So I assumed I should be checking window state in the same way.

Jayson

> On Aug 21, 2017, at 4:49 PM, Kyle Altendorf <sda at fstab.net> wrote:
> 
> On 2017-08-21 16:45, Jayson Kempinger wrote:
> 
>> I'm using PyQt 5.8 and trying to detect a window's state by comparing it to known window states using '&', but every comparison returns False.
>> For example:
>>> if win.windowState() & Qt.WindowNoState:
>>> print("normal")
>>> elif win.windowState() & Qt.WindowMinimized:
>>> print("minimized")
>>> elif win.windowState() & Qt.WindowMaximized:
>>> print("maximized")
>>> else:
>>> raise NotImplementedError
>> However, the comparison is always false (so I hit the exception), even though _print(int(win.windowState()), int(Qt.WindowNoState))_ shows "0 0".
> 
> What is 0 & 0?  0.  Which is False.  :]
> 
>> The same syntax for other QFlags seems to work fine:
>>> if win.windowType() & Qt.Widget:
>>> print("widget")
>>> elif win.windowType() & Qt.Window:
>>> print("window")
>>> else:
>>> raise NotImplementedError
>> My IDE, PyCharm, believes that the & operator is not defined for the WindowStates class and gives me a warning: `_'WindowStates' does not define '__and__', so the '&' operator cannot be used on its instances_`.  I can't seem to find the 5.8 documentation for this enum, but looking at the PyQt4 documentation (http://pyqt.sourceforge.net/Docs/PyQt4/qt-windowstates.html#__and__) it is defined.
>> Is bitwise comparison not available for the WindowStates enum?  Or am I checking it incorrectly?
>> Thanks!
>> Jayson
>> _______________________________________________
>> PyQt mailing list    PyQt at riverbankcomputing.com
>> https://www.riverbankcomputing.com/mailman/listinfo/pyqt



More information about the PyQt mailing list