简体   繁体   English

Pyside2如何打开对话框但并不总是在最上面

[英]Pyside2 how to open the dialog but not always on top

I create a main frame and a pushbutton , 我创建一个主机和一个按钮,

and let button clicked to open a dialog , but the dialog always on top , 并单击按钮以打开一个对话框,但对话框始终位于顶部,

I try to used setWindowsFlag but not work. 我尝试使用setWindowsFlag,但无法正常工作。

from PySide2.QtWidgets import QApplication,QMainWindow,QTabWidget,QWidget
from PySide2.QtWidgets import QMessageBox,QFileDialog,QErrorMessage
from PySide2 import QtCore, QtGui, QtWidgets
class UI_Test20(object):
    def setupUi(self, Test202):
        Test202.setObjectName("Test202")
        Test202.resize(100,100)
        self.centralwidget = QtWidgets.QWidget(Test202)
        self.centralwidget.setObjectName("centralwidget")
        self.pb = QtWidgets.QPushButton(self.centralwidget)
        self.pb.setText('push button!')
        Test202.setCentralWidget(self.centralwidget)
        self.pb.clicked.connect(self.btnClicked)
        self.retranslateUi(Test202)
        QtCore.QMetaObject.connectSlotsByName(Test202)

    def retranslateUi(self, Test202):
        Test202.setWindowTitle(QtWidgets.QApplication.translate("Test202", "MainWindow", None, -1))

    def btnClicked(self):
        ui = Ui_Dialog1(self)
        ui.show()

class Test20(QMainWindow, UI_Test20) :

    def __init__(self, parent):
        super(Test20, self).__init__(parent)
        self.setupUi(self)

and the dialog code 和对话框代码

class Ui_Dialog1(QtWidgets.QDialog):

    def __init__(self, parent=None):
        super(Ui_Dialog1, self).__init__(parent)
        self.p = parent
        self.setupUi(self)

    def setupUi(self, Dialog1):
        Dialog1.setObjectName("Dialog1")
        Dialog1.resize(333, 173)

main 主要

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    ui = Test20(None)
    ui.show()
    sys.exit(app.exec_())

The problem is caused by the fact that the main window is the parent of the QDialog , so it must be removed, but if that is done, the garbage collector will delete it, so the QDialog member of the class must be made: 问题是由以下事实引起的:主窗口是QDialog的父窗口,因此必须将其删除,但是如果这样做,则垃圾收集器将其删除,因此必须创建该类的QDialog成员:

def btnClicked(self):
    self.ui = Ui_Dialog1()
    self.ui.show()

Plus: the correct thing is not to modify the design so I move the connection and the slot associated with the clicked button pb and if we want it to close when the window closes we overwrite the closeEvent() method: 另外:正确的事情是不修改设计,因此我移动了连接和与单击的按钮pb关联的插槽,如果我们希望在窗口关闭时关闭它,我们将覆盖closeEvent()方法:

class Test20(QMainWindow, UI_Test20):
    def __init__(self, parent):
        super(Test20, self).__init__(parent)
        self.setupUi(self)
        self.pb.clicked.connect(self.btnClicked)

    def btnClicked(self):
        self.ui = Ui_Dialog1()
        self.ui.show()

    def closeEvent(self, *args, **kwargs):
        self.ui.close()
        QMainWindow.closeEvent(self, *args, **kwargs)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM