开发者

QT4: How to restart application? Reset settings? [duplicate]

This question already has answers here: how to restart my own qt application? (10 answers) 开发者_如何学Python Closed 6 years ago.

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


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());


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.


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.


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


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜