ウィンドウを他のボタンの上に配置するボタンを作成しようとしています。他の質問からの推奨事項を使用して、フラグsetWindowFlags(self.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
を設定および削除するためにクラスに入れました。setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowStaysOnTopHint)
フラグを設定しますが、ボタンの状態を変更しても、そのフラグは有効のままです。コード例は次のとおりです。
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()