Rao
Rao

Reputation: 2962

How to Restart PyQt4 Application

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.

Upvotes: 1

Views: 5400

Answers (4)

doru
doru

Reputation: 9110

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.

Upvotes: 0

rchuso
rchuso

Reputation: 101

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.

Upvotes: 3

Mahendra
Mahendra

Reputation: 333

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 !!

Upvotes: 2

Fábio Diniz
Fábio Diniz

Reputation: 10353

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

Upvotes: 0

Related Questions