QT4: How to restart application? Reset settings? [

2019-07-16 05:35发布

This question already has an answer here:

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

6条回答
在下西门庆
2楼-- · 2019-07-16 05:46

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.

查看更多
够拽才男人
3楼-- · 2019-07-16 05:52

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());
查看更多
ゆ 、 Hurt°
4楼-- · 2019-07-16 06:00

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

查看更多
男人必须洒脱
5楼-- · 2019-07-16 06:02

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.

查看更多
6楼-- · 2019-07-16 06:02

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.

查看更多
仙女界的扛把子
7楼-- · 2019-07-16 06:05

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
查看更多
登录 后发表回答