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

Jayson Kempinger jkempinger at decisionvis.com
Mon Aug 21 21:45:18 BST 2017


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".

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__ <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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20170821/3f0a7fc8/attachment.html>


More information about the PyQt mailing list