假设你在MainWindow.定义在__init__
MainWindow.EXIT_CODE_REBOOT = -12345678 # or whatever number not already taken
您的广告位restart()
应包含:
qApp.exit( MainWindow.EXIT_CODE_REBOOT )
和你的main
:
currentExitCode = MainWindow.EXIT_CODE_REBOOT while currentExitCode == MainWindow.EXIT_CODE_REBOOT: a = QApplication(sys.argv) w = MainWindow() w.show() currentExitCode = a.exec_() return currentExitCode
[1] https://wiki.qt.io/How_to_make_an_Application_restartable
我只是重新实现了这个keyPressedEvent
方法而不是信号/插槽.
import sys
from PyQt4 import QtGui
from PyQt4.QtCore import Qt
class MainWindow(QtGui.QMainWindow):
EXIT_CODE_REBOOT = -123
def __init__(self,parent=None):
QtGui.QMainWindow.__init__(self, parent)
def keyPressEvent(self,e):
if (e.key() == Qt.Key_R):
QtGui.qApp.exit( MainWindow.EXIT_CODE_REBOOT )
if __name__=="__main__":
currentExitCode = MainWindow.EXIT_CODE_REBOOT
while currentExitCode == MainWindow.EXIT_CODE_REBOOT:
a = QtGui.QApplication(sys.argv)
w = MainWindow()
w.show()
currentExitCode = a.exec_()
a = None # delete the QApplication object