Search code examples
pythonpyqtbit-manipulationflagsalways-on-top

How to test if WindowStaysOnTopHint flag is set in windowFlags?


Is this supposed to return a boolean value?

>>> win.windowFlags() & QtCore.Qt.WindowStaysOnTopHint
<PyQt4.QtCore.WindowFlags object at 0x7ad0578>

I already knew that

# enable always on top
win.windowFlags() | QtCore.Qt.WindowStaysOnTopHint

# disable it
win.windowFlags() & ~QtCore.Qt.WindowStaysOnTopHint

# toggle it 
win.windowFlags() ^ QtCore.Qt.WindowStaysOnTopHint

Solution

  • The WindowFlags object is an OR'd together set of flags from the WindowType enum. The WindowType is just a subclass of int, and the WindowFlags object also supports int operations.

    You can test for the presence of a flag like this:

    >>> bool(win.windowFlags() & QtCore.Qt.WindowStaysOnTopHint)
    False
    

    or like this:

    >>> int(win.windowFlags() & QtCore.Qt.WindowStaysOnTopHint) != 0
    False
    

    In general, & returns the value itself when present, or zero when absent:

    >>> flags = 1 | 2 | 4
    >>> flags
    7
    >>> flags & 2
    2
    >>> flags & 8
    0