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()