回答
看看How to restart an application線程qtcentre.org,其中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;
}
1.你不應該在'delete'之前檢查'NULL',2.你可以在循環中聲明'QPointer's,那麼你根本不需要刪除它們(作爲智能指針和全部), 3.你根本不需要指針,4.你有一個語法錯誤(檢查我的答案)。 – rubenvb 2011-06-15 10:25:08
當我聲明main_window = new MainWindow時,此代碼給出「退出代碼-1073741819」錯誤;當聲明main_window = new MainWindow(app)時,它會發出聲明錯誤。 – abhishek 2012-04-23 19:10:22
這是我引用的原始答案。看看'MainWindow'類的參數類型是什麼,並記住'app'變量的類型是'QPointer
我只是用上述方法,我注意到,在重新啓動我的應用程序崩潰。 ...然後我切換下面的代碼行:
if(app) delete app;
if(main_window) delete main_window;
到:
if(main_window) delete main_window;
if(app) delete app;
和它的行爲確定。出於某種原因,窗口必須先刪除。 只是爲未來的讀者一個筆記。
編輯: ...和誰想要一個真正的進程重啓了不同的方法:你可以在你的QApplication的子類,聲明對myApp ::重啓()方法。以下版本工程確定在兩個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
}
刪除nullptr;很好,不需要檢查 – paulm 2013-03-12 21:07:51
謝謝@oaulm,這是正確的(我只是基於我對上面Piotr Dobrogost發佈的原始代碼的評論) – 2013-03-13 12:01:11
我拿別人的答案的解決方案,但更好。不需要指針,但需要在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;
}
要重新啓動應用程序,請嘗試:
#include <QApplication>
#include <QProcess>
...
// restart:
qApp->quit();
QProcess::startDetached(qApp->arguments()[0], qApp->arguments());
像魅力一樣工作,謝謝! – 2016-04-06 09:08:08
除了冗餘的第一個參數外,這很好用,所以我編輯了答案以從參數列表中刪除程序名稱。謝謝! – 2016-09-27 00:22:23
上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
假設是你的重啓代碼:
main.cxx
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);
下面是代碼:
main.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;
}
mainwindow.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顯示的信號插槽
mainwindow。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()));
}
最後,你需要發送信號(in你的代碼,你需要的部分),以這樣的方式
actionReboot->trigger();
我做我的代碼顯示以下說明:How to make an application restartable - Qt Wiki
儘管上述更改有效,但重新啓動的應用程序有時會崩潰,webkitwidget在重新啓動的應用程序中無法正常工作。由@deepmax建議的解決方案適合我。 – 2016-12-23 08:47:10
做一個真正的進程重啓沒有子:
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);
在你的代碼。
- 1. 重新啓動自己的程序?
- 2. 如何在VC++中重新啓動我自己的應用程序
- 3. 如何重新啓動qt中的應用程序?
- 4. 自動更新後重新啓動我的應用程序?
- 5. node.js應用程序可以重新啓動自己
- 6. 如何從我自己的android應用程序啓動電報應用程序?
- 7. 如何在崩潰後重新啓動qt應用程序?
- 8. 重新啓動我的應用程序
- 9. 如何重新啓動我的C#移動應用程序?
- 10. 如何在Windows環境中自動啓動我自己的應用程序?
- 11. 重新安裝運行Android應用程序自己的APK,然後重新啓動應用程序?
- 12. 如何爲自己託管的應用程序啓用自動更新功能
- 13. 如何讓您的java應用程序自行重新啓動
- 14. 如何自動重新啓動我的應用程序或服務?
- 15. 重新啓動時自動啓動應用程序
- 16. 應用程序不重新啓動的設備在Qt
- 17. 我的應用程序如何重新啓動?
- 18. 如何從我自己的應用程序開啓NFC
- 19. 重新啓動應用程序時的流星自動刷新
- 20. 應用程序何時重新啓動?
- 21. 終端如何控制自己啓動的應用程序
- 22. jenkins slave agent重啓後如何重新啓動應用程序
- 23. 每當我重新啓動Rails應用程序時,如何自動填充memcached?
- 24. 創建我自己的Android應用程序/啓動器
- 25. 從我自己的應用程序內啓動Apple Mail App?
- 26. BroadcastReceiver可以啓動我自己的應用程序?
- 27. 我如何導入下一章重新啓動應用程序?
- 28. QT4:如何重新啓動應用程序?重新設置?
- 29. 市場更新後自動重新啓動應用程序
- 30. 鉻/鉻應用程序更新後自動重新啓動
「重啓」是什麼意思? – 2011-02-26 21:16:15
相關:[QT4:如何重新啓動應用程序?重置設置?](http://stackoverflow.com/questions/5127600/) – 2011-02-27 11:50:26
@JeremiahWillcock在這種情況下可以解釋「重啓」的方式有多少? – 2014-07-29 22:15:58