pysidepyside2flagsqwidgetqmainwindow

Pyside Remove window flags


Im designing a Pyside Qt application and I want to toggle the QtCore.Qt.WindowStaysOnTopHint window flag in my main window. Setting this hint using this code works fine:

self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
self.show()

but I can't work out how to remove a window flag using Pyside.

Does anybody know how to do this?


Solution

  • UPDATE:

    As of Qt-5.9, there's now a simple convenience API that does exactly what the original solution below does (see setWindowFlag [singular]):

    # add a flag to the existing flags
    self.setWindowFlag(QtCore.Qt.WindowStaysOnTopHint, True)
    ...
    # remove a flag from the existing flags
    self.setWindowFlag(QtCore.Qt.WindowStaysOnTopHint, False)
    ...
    # re-show the window
    self.show()
    

    Note that the last line is always necessary, since re-setting the window flags will also reset the widget's parent - which means the widget must be made visible again by explicitly re-showing it (see windowFlags).

    Of course, you can still use setWindowFlags [plural], to set and unset multiple flags using the bitwise operators.


    ORIGINAL SOLUTION:

    Window flags would normally be OR'd together with the existing flags:

        print(int(self.windowFlags()))
        self.setWindowFlags(self.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
        print(int(self.windowFlags()))
    

    Then to remove the flag, AND it out using the flag's negation:

        self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowStaysOnTopHint)
        print(int(self.windowFlags()))