[英]how to restart my own qt application?
我只是問自己如何重新啟動我自己的 qt 應用程序?
有人可以給我舉個例子嗎?
要重新啟動應用程序,請嘗試:
#include <QApplication>
#include <QProcess>
...
// restart:
qApp->quit();
QProcess::startDetached(qApp->arguments()[0], qApp->arguments());
我正在接受其他答案的解決方案,但更好。 不需要指針,但需要一個;
在do { ... } while( ... );
的while
語句之后構造。
int main(int argc, char *argv[])
{
const int RESTART_CODE = 1000;
do
{
QApplication app(argc, argv);
MainWindow main_window(app);
} while( app.exec() == RESTART_CODE);
return return_from_event_loop_code;
}
假設1337是您的重啟代碼:
主文件
int main(int argc, char * argv[])
{
int result = 0;
do
{
QCoreApplication coreapp(argc, argv);
MyClass myObj;
result = coreapp.exec();
} while( result == 1337 );
return result;
}
myClass.cxx
qApp->exit(1337);
在沒有子類化的情況下重新啟動真正的進程:
QCoreApplication a(argc, argv);
int returncode = a.exec();
if (returncode == -1)
{
QProcess* proc = new QProcess();
proc->start(QCoreApplication::applicationFilePath());
}
return returncode;
像前面的示例一樣針對 Mac OS 進行編輯。
重新開始通話
QCoreApplication::exit(-1);
在您的代碼中的某處。
在 qtcentre.org 上查看How to restart an application thread, muisei給出了這段代碼
#define RESTART_CODE 1000
int main(int argc, char *argv[])
{
int return_from_event_loop_code;
QPointer<QApplication> app;
QPointer<MainWindow> main_window;
do
{
if(app) delete app;
if(main_window) delete main_window;
app = new QApplication(argc, argv);
main_window = new MainWindow(app);
return_from_event_loop_code = app->exec();
}
while(return_from_event_loop_code==RESTART_CODE)
return return_from_event_loop_code;
}
要重新啟動正在運行的 Qt 應用程序(至少在 Qt 5.15.2 中),您可以執行以下操作:
#include <QApplication>
#include <QProcess>
//...
QString program = qApp->arguments()[0];
QStringList arguments = qApp->arguments().mid(1); // remove the 1st argument - the program name
qApp->quit();
QProcess::startDetached(program, arguments);
Rubenvb 想法的這種輕微變化適用於 PyQt。 clearSettings
是觸發重啟的方法。
class GuiMain
#Most of implementation missing
def clearSettings(self):
#Clearing the settings missing
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
這是代碼:
主.cpp:
int main(int argc, char *argv[])
{
int currentExitCode = 0;
do {
QApplication a(argc, argv);
MainWindow w;
w.show();
currentExitCode = a.exec();
} while( currentExitCode == MainWindow::EXIT_CODE_REBOOT );
return currentExitCode;
}
主窗口.h
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
static int const EXIT_CODE_REBOOT;//THIS IS THE IMPORTANT THING TO ADD TO YOUR CODE
~MainWindow();
private slots:
void slotReboot();//AND THIS ALSO
//ALL THE OTHER VARIABLES
}
slotReboot()
是將接收QAction
信號的插槽,我將在 mainwindow.cpp 中顯示
主窗口.cpp
首先初始化EXIT_CODE_REBOOT
:
int const MainWindow::EXIT_CODE_REBOOT = -123456789;
並聲明一個QAction
指針:
QAction* actionReboot;
然后在MainWindow
構造函數中:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
actionReboot = new QAction( this );
actionReboot->setText( tr("Restart") );
actionReboot->setStatusTip( tr("Restarts the application") );
connect( actionReboot, SIGNAL (triggered()),this, SLOT (slotReboot()));
}
最后,您需要以這種方式發送信號(在您需要的代碼部分):
actionReboot->trigger();
我按照以下說明執行了我顯示的代碼: How to make an application restartable - Qt Wiki
我剛剛使用了上面描述的方法,我注意到我的應用程序在重新啟動時崩潰了。 ...然后我切換了以下代碼行:
if(app) delete app;
if(main_window) delete main_window;
到:
if(main_window) delete main_window;
if(app) delete app;
它的行為正常。 由於某種原因,必須先刪除窗口。 只是給未來的讀者一個注釋。
編輯: ...對於那些想要真正重新啟動進程的人來說,還有一種不同的方法:您可以在 QApplication 的子類中聲明一個 myApp::Restart() 方法。 以下版本在 MS-Windows 和 MacOS 上都可以正常工作:
// Restart Application
void myApp::Restart(bool Abort)
{
// Spawn a new instance of myApplication:
QProcess proc;
#ifdef Q_OS_WIN
proc.start(this->applicationFilePath());
#endif
#ifdef Q_OS_MAC
// In Mac OS the full path of aplication binary is:
// <base-path>/myApp.app/Contents/MacOS/myApp
QStringList args;
args << (this->applicationDirPath() + "/../../../myApp.app");
proc.start("open", args);
#endif
// Terminate current instance:
if (Abort) // Abort Application process (exit immediattely)
::exit(0);
else
this->exit(0); // Exit gracefully by terminating the myApp instance
}
你可以使用我的開源庫:
https://marketplace.qt.io/products/main-loop-wdt-for-qt-qml
這是主qt循環的看門狗定時器,但我有一個強制重啟的功能,有不同的策略:startdetached + exit,Linux / macOS上的exec系統調用,延遲重啟(例如,退出並在3秒后重啟)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.