2

1.) I would like to restart my QT4 application. Just a normal shutdown and start of the same application.

2.) Why? Well i need an Option to "reset" everything. To restart the application seems to be the easiest way to do this. The problem is, that there are a LOT of classes and everything. I dont have the time to put every setting of them back to standard, every textBox, Widget to clear... I Know application restart is not the best way, what do you think is there another way?

Thank You

Revollt
  • 55
  • 3
  • 5

6 Answers6

4

For restarting an application you can use startDetached after quiting the process:

#include <QApplication>
#include <QProcess>

...

// restart the app:
qApp->quit();
QProcess::startDetached(qApp->arguments()[0], qApp->arguments());
masoud
  • 55,379
  • 16
  • 141
  • 208
2

1) You can run a Script, Schedule the OS to start your app later.

2) Write a separate class which contains all your application Settings. Reset whenever required.

MSKOnline
  • 21
  • 3
2

Funny request. Just reserve an exit code for "restart" and do something like (untested):

int main(int argc, char **argv)
{
 QApplication app(argc, argv);
 ...
 int ret = app.exec();
 if (ret == EXIT_RESTART) {
   ::execve(...);
 }
 return ret;
}

Then you can just call QApplication::exit(EXIT_RESTART) anywhere and you should be good to go. Or use a wrapper script to do the same. (Make sure in both cases that you handle command line arguments satisfactorily if you app takes any.)

A cleaner approach would be to connect all the things that need to be reset to the same signal.

Mat
  • 202,337
  • 40
  • 393
  • 406
  • You can also use a "try-catch" here instead of a return code. When you think it is time to restart, throw a known exception. It has a benefit of allowing you to restart even in crash conditions. – j4x Mar 04 '11 at 13:18
  • Qt doesn't mix well with exceptions in the general case. Some usages are safe, but it's not recommended as far as I know. See [Qt exception safety](http://doc.qt.nokia.com/4.6/exceptionsafety.html). – Mat Mar 04 '11 at 15:03
1

you could delete the classes and create new ones in main() under the same QApplication

yolo
  • 2,757
  • 6
  • 36
  • 65
1

The sane thing to do in such a case is to put all the stuff that creates/initializes widgets, etc., in a single function (of course, it can call sub-functions). When you need to reset everything, simply call it. Depending on the exact implementation, you may need to delete/un-initialize the stuff first.

Daggerstab
  • 623
  • 5
  • 7
0

This method works on PyQt. I wrote it for erasing all settings and to restart the application with clean settings. application_main is the main method, and clearSettings is the slot that clears the settings.

class GuiMain

    #Most of implementation missing

    def clearSettings(self):
        """Deletes all settings, and restarts the application"""
        #TODO: save changes
        setting_store = QSettings()
        setting_store.clear()
        setting_store.sync()
        QApplication.exit(GuiMain.restart_code)

    restart_code = 1000

    @staticmethod
    def application_main():
        """
        The application's main function. 
        Create application and main window and run them.
        """
        while True:
            app = QApplication(sys.argv)
            window = GuiMain()
            window.show()
            ret = app.exec_()
            if ret != GuiMain.restart_code:
                break
            del window
            del app
Eike
  • 2,205
  • 17
  • 10