如果对话框由大型机打开,我如何正确关闭对话框
How do I close the dialog properly , if the dialog is opened by mainframe
我有一个大型机并单击按钮打开一个新对话框,
对话框有一个计时器,让计时器以 3 秒的间隔显示消息,
但是如果我点击右上角的关闭按钮关闭对话框,消息框仍然运行,
如何避免?
谢谢!
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):
self.ui = Ui_Dialog1(self)
self.ui.show()
class Test20(QMainWindow, UI_Test20) :
def __init__(self, parent):
super(Test20, self).__init__(parent)
self.setupUi(self)
和对话框代码
class Ui_Dialog1(QtWidgets.QDialog):
def __init__(self, parent=None):
super(Ui_Dialog1, self).__init__(parent)
self.p = parent
self.setupUi(self)
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.update)
self.timer.start(3000)
def update(self):
QMessageBox.information(self,'123','123')
def setupUi(self, Dialog1):
Dialog1.setObjectName("Dialog1")
Dialog1.resize(333, 173)
主要
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
ui = Test20(None)
ui.show()
sys.exit(app.exec_())
当您使用 QDialog 调用 show() 方法时,您的对话框将是无模式的,并且在关闭时将被隐藏而不是被删除。为确保您的对话已删除,请在您的对话初始化方法中再添加一行:
def __init__(self, parent=None):
super(Ui_Dialog1, self).__init__(parent)
self.p = parent
self.setupUi(self)
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.update)
self.timer.start(3000)
#Make sure that your dialog will be deleted not hidden
self.setAttribute(QtGui.Qt.WA_DeleteOnClose)
我有一个大型机并单击按钮打开一个新对话框,
对话框有一个计时器,让计时器以 3 秒的间隔显示消息,
但是如果我点击右上角的关闭按钮关闭对话框,消息框仍然运行,
如何避免?
谢谢!
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):
self.ui = Ui_Dialog1(self)
self.ui.show()
class Test20(QMainWindow, UI_Test20) :
def __init__(self, parent):
super(Test20, self).__init__(parent)
self.setupUi(self)
和对话框代码
class Ui_Dialog1(QtWidgets.QDialog):
def __init__(self, parent=None):
super(Ui_Dialog1, self).__init__(parent)
self.p = parent
self.setupUi(self)
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.update)
self.timer.start(3000)
def update(self):
QMessageBox.information(self,'123','123')
def setupUi(self, Dialog1):
Dialog1.setObjectName("Dialog1")
Dialog1.resize(333, 173)
主要
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
ui = Test20(None)
ui.show()
sys.exit(app.exec_())
当您使用 QDialog 调用 show() 方法时,您的对话框将是无模式的,并且在关闭时将被隐藏而不是被删除。为确保您的对话已删除,请在您的对话初始化方法中再添加一行:
def __init__(self, parent=None):
super(Ui_Dialog1, self).__init__(parent)
self.p = parent
self.setupUi(self)
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.update)
self.timer.start(3000)
#Make sure that your dialog will be deleted not hidden
self.setAttribute(QtGui.Qt.WA_DeleteOnClose)