自分のqtアプリケーションを再起動する方法を自問していますか?
誰かが私に例を見せてもらえますか?
アプリケーションを再起動するには、次を試してください。
#include <QApplication>
#include <QProcess>
...
// restart:
qApp->quit();
QProcess::startDetached(qApp->arguments()[0], qApp->arguments());
私は他の答えの解決策を取っていますが、より良いです。ポインターは必要ありませんが、構文のステートメントの;
後に必要があります。while
do { ... } 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が再起動コードであると仮定します。
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);
サブクラス化せずに実際のプロセスを再起動する:
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でアプリケーションスレッドを再起動する方法を見てください。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;
}
コードは次のとおりです。
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;
}
メインウィンドウ.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
}
これは、mainwindow.cpp に表示slotReboot()
する信号を受信するスロットです。QAction
メインウィンドウ.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();
これらの指示に従って、私が示したコードを実行しました:アプリケーションを再起動可能にする方法 - Qt Wiki
この 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
上記の方法を使用したところ、再起動時にアプリケーションがクラッシュすることに気付きました。...次に、次のコード行を切り替えました。
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
}