如何重新启动我自己的qt应用程序?
我只是问自己如何重新启动我自己的 qt 应用程序?
有人可以给我举个例子吗?
i just asking myself how to restart my own qt application?
Can somebody please show me an example?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
要重新启动正在运行的 Qt 应用程序(至少在 Qt 5.15.2 中),您可以执行以下操作:
To restart a running Qt application (at least in Qt 5.15.2) you can do the following:
我正在采用其他答案解决方案,但更好。不需要指针,但在
do { ... } while( ... );< 的
while
语句后面需要一个;
/代码>构造。I'm taking the other answers solutions, but better. No need for pointers, but there is a need for a
;
after thewhile
statement of ado { ... } while( ... );
construct.假设1337是您的重启代码:
main.cxx
myClass.cxx
Assuming that 1337 is your restart code:
main.cxx
myClass.cxx
在没有子类化的情况下进行真正的进程重新启动:
像前面的示例一样针对 Mac OS 进行编辑。
重新启动
代码中某处的调用。
Doing a real process restart without subclassing:
Edit for Mac OS like earlier example.
To restart call
somewhere in your code.
查看如何重新启动应用程序线程qtcentre.org,其中 muisei 给出了此代码
Take a look at How to restart an application thread on qtcentre.org, where muisei gives this code
您可以使用我的开源库:
https://marketplace。 qt.io/products/main-loop-wdt-for-qt-qml
这是主 qt 循环的看门狗计时器,但我有一个强制重新启动的功能,具有不同的策略:startdetached + exit,exec system Linux/macOS上调用,以及延迟重启(例如3秒后退出重启)
You can use my open source library:
https://marketplace.qt.io/products/main-loop-wdt-for-qt-qml
It's a watchdog timer for the main qt loop, but I have a function for forced reboot, with different strategies: startdetached + exit, exec system call on Linux / macOS, and delayed restart (for example, exit and restart after 3 seconds)
我刚刚使用了上面描述的方法,我注意到我的应用程序在重新启动时崩溃了。
...然后我将以下代码行:
切换为:
并且它的行为正常。由于某种原因,必须先删除该窗口。
只是给未来读者的注释。
编辑: ...对于那些想要真正的进程重新启动的人来说,还有一种不同的方法:您可以在 QApplication 的子类中声明 myApp::Restart() 方法。以下版本在 MS-Windows 和 Windows 上都可以正常工作。苹果系统:
I just used the method described above and I noticed that my application crashes on restart.
...then I switched the following lines of code:
to:
and it behaves OK. For some reason the window must be deleted first.
Just a note for future readers.
EDIT: ...and a different approach for those who want a real process-restart: You can declare a myApp::Restart() method in your subclass of QApplication. The following version works OK on both MS-Windows & MacOS:
Rubenvb 的想法略有不同,适用于 PyQt。
clearSettings
是触发重启的方法。This slight variation on Rubenvb's idea works with PyQt.
clearSettings
is the method that triggers the restart.代码如下:
main.cpp:
mainwindow.h
slotReboot()
是将接收信号的插槽QAction
我将在 mainwindow.cpp 中显示mainwindow.cpp
首先初始化
EXIT_CODE_REBOOT
:并声明一个
QAction
指针:然后在
MainWindow
构造函数中:最后,您需要发送信号(在您需要的代码部分中),这样:
我按照以下说明执行了我显示的代码:如何使应用程序可重新启动 - Qt Wiki
Here is the code:
main.cpp:
mainwindow.h
The
slotReboot()
is the slot that will receive the signal of theQAction
I'm going to show in the mainwindow.cppmainwindow.cpp
First initialize
EXIT_CODE_REBOOT
:and declare a
QAction
pointer:then in the
MainWindow
constructor:And finally you need to send the signal (in the part of your code you need), in this way:
I did the code I showed following these instructions: How to make an application restartable - Qt Wiki
要重新启动应用程序,请尝试:
To restart application, try: