1

I'm trying to make a button which would place window on top of others. Using recommends from other questions, I put in my class setWindowFlags(self.windowFlags() | QtCore.Qt.WindowStaysOnTopHint) to set and setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowStaysOnTopHint) to delete flag. It sets the flag, but when i change the button state, it still has that flag enabled. Here is code example:

from PyQt5 import QtWidgets, QtCore
import sys

class widget(QtWidgets.QWidget):
    def __init__(self):
        QtWidgets.QWidget.__init__(self)

        self.resize(500, 500)
        box = QtWidgets.QVBoxLayout()
        self.setLayout(box)

        self.btn = QtWidgets.QPushButton("pin")
        box.addWidget(self.btn)
        self.btn.setCheckable(True)
        self.btn.toggled.connect(self.setOnTop)

    def setOnTop(self):
        if self.btn.isChecked():
            self.setWindowFlags(self.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
            print("checked")
        else:
            self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowStaysOnTopHint)
            print("unchecked")
        self.show()

def main(self):
    app = QtWidgets.QApplication(sys.argv)
    ex = widget()
    ex.show()
    sys.exit(app.exec_())

main()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Artem
  • 563
  • 3
  • 17
  • Works for me on 18.10 with PyQt 5.11.2. You might also try using the simpler setWindowFlag api http://doc.qt.io/qt-5/qwidget.html#setWindowFlag added in 5.9 ie, self.setWindowFlag(QtCore.Qt.WindowStaysOnTopHint, True) to set and self.setWindowFlag(QtCore.Qt.WindowStaysOnTopHint, False) to clear. Also your main shouldn't have the self argument. – shao.lo Nov 04 '18 at 17:58
  • @shao.lo, thanks for the answer. Unfortunatelly, it doesn't work. I also tried to run it under Wayland and Xorg and it doesn't work too. Is there a need to set `X11BypassWindowManagerHint` flag? – Artem Nov 04 '18 at 19:54
  • hmm, that works under windows 7. Is there any way to fix this issue under x11? – Artem Nov 10 '18 at 16:16
  • It is most likely a bug. You could try using a virtual environment and testing different versions of pyqt5. – shao.lo Nov 10 '18 at 18:22

0 Answers0