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

安稳与你 提交于 2019-12-11 03:26:01

问题


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


回答1:


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



回答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.




回答3:


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.




回答4:


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




回答5:


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.




回答6:


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


来源:https://stackoverflow.com/questions/5127600/qt4-how-to-restart-application-reset-settings

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