QT4:如何重新启动应用程序?重置设置?

发布于 2024-10-19 11:47:59 字数 190 浏览 2 评论 0原文

1.) 我想重新启动我的 QT4 应用程序。只是同一应用程序的正常关闭和启动。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

骷髅 2024-10-26 11:48:00

要重新启动应用程序,您可以在退出进程后使用 startDetached

#include <QApplication>
#include <QProcess>

...

// restart the app:
qApp->quit();
QProcess::startDetached(qApp->arguments()[0], qApp->arguments());

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());
妞丶爷亲个 2024-10-26 11:48:00

1) 您可以运行脚本,安排操作系统稍后启动您的应用程序。

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.

半边脸i 2024-10-26 11:48:00

有趣的请求。只需保留“重新启动”的退出代码并执行类似(未经测试)的操作:

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

然后您可以在任何地方调用 QApplication::exit(EXIT_RESTART) ,您应该可以开始了。或者使用包装脚本来执行相同的操作。 (如果您的应用程序需要任何命令行参数,请确保在这两种情况下都能令人满意地处理命令行参数。)

更简洁的方法是将所有需要重置的内容连接到同一信号。

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.

り繁华旳梦境 2024-10-26 11:48:00

您可以删除这些类并在同一 QApplication 下的 main() 中创建新类

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

梦晓ヶ微光ヅ倾城 2024-10-26 11:48:00

在这种情况下,明智的做法是将创建/初始化小部件等的所有内容放在单个函数中(当然,它可以调用子函数)。当您需要重置所有内容时,只需调用它即可。根据具体的实现,您可能需要首先删除/取消初始化这些内容。

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.

别忘他 2024-10-26 11:48:00

该方法适用于 PyQt。我编写它是为了擦除所有设置并使用干净的设置重新启动应用程序。 application_main 是主方法,clearSettings 是清除设置的槽。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文