我使用下面的代码关闭Qwidget窗口自动经过一段时间
class ErrorWindow2(QtGui.QWidget):
def __init__( self ):
QtGui.QWidget.__init__( self, None, QtCore.Qt.WindowStaysOnTopHint)
msgBox = QMessageBox(self)
msgBox.move (500,500)
msgBox.setIcon(QMessageBox.Critical)
msgBox.setText("Test 2")
msgBox.setWindowTitle("ERROR")
msgBox.setStandardButtons(QMessageBox.Ok)
self.errWin2Timer = QtCore.QTimer()
self.errWin2Timer.timeout.connect(self.closeBox)
self.errWin2Timer.setSingleShot(True)
self.errWin2Timer.start(10000)
ret = msgBox.exec_()
if ret == QtGui.QMessageBox.Ok:
return
else:
return
def closeBox(self):
self.close()
def closeEvent(self, event):
logger.debug("Reached Error window 1 close event")
if self.errWin2:
self.errWin2.stop()
self.errWin2.deleteLater()
event.accept()
但问题是self.close
不起作用。一段时间后自动关闭窗口的最佳方法是什么
问题是,当您在构造函数完成执行之前放置
ret = msgBox.exec_()
时,窗口对象尚未完成构建,因此没有要关闭的对象,因此当对话框关闭时,将显示刚刚打开的窗口。我完成了建筑。我们的想法是完成窗口的构建,然后调用ret = msgBox.exec_()
,为此我们将使用QTimer.singleShot()
另一方面,
closeEvent
方法是不必要的,因为我正在尝试这样做。IMHO是从内存中消除self.errWin2Timer
(虽然似乎是因为使用errWin2
而不是errWin2Timer
而出现了一个拼写错误),但是作为窗口的子窗口是没有必要的,因为在Qt中,如果父窗口死了,子窗口也会死相关问题 更多 >
编程相关推荐