How to Restart PyQt4 Application

元气小坏坏 提交于 2019-12-11 08:38:12

问题


Is there a way to restart PyQt application QApplication

I have an app created with pyqt4 and python 2.6 using below code

app = QtGui.QApplication(sys.argv)

i have settings options where i set some settings. Now when i save settings i need to reload the application so that new settings are effected. Without the need of end user to exit and launch the app again.


回答1:


I had a similar problem and simply used this at the appropriate place:

subprocess.Popen([__file__])
sys.exit(0)

It was a simple application, and didn't need any further arguments.




回答2:


I explain how I did it :

I create a extra one file main.py which calls my actual main program file dash.py. And I emits a signal for restarting (my programs auto updates at the closeEvent) so I required to emit a signal for it. This is the snippets hope this will help you.

This one is in my main program file in dash.py

def restart(self):
    # create a signal equivalent to "void someSignal(int, QWidget)"
    self.emit(QtCore.SIGNAL("RESTARTREQUIRED"), True)

This one in main.py which calls actual program only and restarts the app

import sys
from PyQt4 import QtGui,QtCore
from bin import dash

if __name__ == "__main__":
    application = QtGui.QApplication(sys.argv)
    uDesk = dash.app()
    uDesk.show()
    uDesk.actionRestart.triggered.disconnect()
    # define restart slot
    @QtCore.pyqtSlot()
    def restartSlot():
        print 'Restarting app'
        global uDesk
        uDesk.deleteLater()
        uDesk = dash.app()
        uDesk.show()
        uDesk.actionRestart.triggered.disconnect()   
        uDesk.actionRestart.triggered.connect(restartSlot)
        print 'New app started !'

    QtCore.QObject.connect(uDesk,
                   QtCore.SIGNAL("RESTARTREQUIRED"),
                   restartSlot)
    uDesk.actionRestart.triggered.connect(restartSlot)
    sys.exit(application.exec_()) 

Hope this was helpful !!




回答3:


EDIT: Changing the way to get the application path

You could just start a new process and exit yours, something like this: (CODE NOT TESTED, but based on this answer)

// Restart Application
def restart(self, abort):
    // Spawn a new instance of myApplication:
    proc = QProcess()
    //proc.start(self.applicationFilePath());
    import os
    proc.start(os.path.abspath(__file__))

    self.exit(0);

Code it as a method of your Qapplication or even a function if you don't feel like subclassing




回答4:


This is how I restart TicTacToe game in PySide (it should be the same in PyQt):

I have a single class - a QWidget class - in which is coded the Tic Tac Toe game. To restart the application I use:

  1. import subprocess

  2. a QPushButton() like so:

    self.button = QPushButton("Restart", self)

  3. the connection of the button to Slot:

    self.buton.clicked.connect(self.restartGame)

  4. the Slot for this button, like so:

    def restartGame(self): self.close() subprocess.call("python" + " TicTAcToe.py", shell=True)

All these are in the same - single - class. And what these do: close the active window of the game and create a new one.

How this code looks in the TicTacToe class:

import subprocess
class TicTacToe(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        self.button = QPushButton("Restart", self)
        self.buton.clicked.connect(self.restartGame)
    def restartGame(self):
        self.close()
        subprocess.call("python" + " TicTacToe.py", shell=True)

def main():
    app = QApplication(sys.argv)
    widget = TicTacToe()
    widget.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

EDIT

I know this doesn't answer the question (it doesn't restart a QApplication), but I hope this helps those who want to restart their QWidget single class.



来源:https://stackoverflow.com/questions/15221361/how-to-restart-pyqt4-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!