如何重新启动我自己的qt应用程序?

发布于 2024-10-19 12:40:03 字数 53 浏览 10 评论 0原文

我只是问自己如何重新启动我自己的 qt 应用程序?

有人可以给我举个例子吗?

i just asking myself how to restart my own qt application?

Can somebody please show me an example?

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

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

发布评论

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

评论(10

夜夜流光相皎洁 2024-10-26 12:40:04

要重新启动正在运行的 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);

To restart a running Qt application (at least in Qt 5.15.2) you can do the following:

#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);
泪是无色的血 2024-10-26 12:40:04

我正在采用其他答案解决方案,但更好。不需要指针,但在 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;
}

I'm taking the other answers solutions, but better. No need for pointers, but there is a need for a ; after the while statement of a do { ... } while( ... ); construct.

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;
}
烟酒忠诚 2024-10-26 12:40:04

假设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);

Assuming that 1337 is your restart code:

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);
傲世九天 2024-10-26 12:40:04

在没有子类化的情况下进行真正的进程重新启动:

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);

代码中某处的调用。

Doing a real process restart without subclassing:

QCoreApplication a(argc, argv);
int returncode = a.exec();
if (returncode == -1)
{
  QProcess* proc = new QProcess();
  proc->start(QCoreApplication::applicationFilePath());
}
return returncode;

Edit for Mac OS like earlier example.

To restart call

QCoreApplication::exit(-1);

somewhere in your code.

遇见了你 2024-10-26 12:40:04

查看如何重新启动应用程序线程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;
}

Take a look at How to restart an application thread on qtcentre.org, where muisei gives this code

#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;
}
如梦 2024-10-26 12:40:04

您可以使用我的开源库:

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)

短暂陪伴 2024-10-26 12:40:04

我刚刚使用了上面描述的方法,我注意到我的应用程序在重新启动时崩溃了。
...然后我将以下代码行:

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 和 Windows 上都可以正常工作。苹果系统:

// 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
}

I just used the method described above and I noticed that my application crashes on restart.
...then I switched the following lines of code:

if(app) delete app;
if(main_window) delete main_window;

to:

if(main_window) delete main_window;
if(app) delete app;

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:

// 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
}
心房敞 2024-10-26 12:40:04

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

This slight variation on Rubenvb's idea works with PyQt. clearSettings is the method that triggers the restart.

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
尽揽少女心 2024-10-26 12:40:04

代码如下:

ma​​in.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;

}

ma​​inwindow.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 中显示

ma​​inwindow.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

Here is the 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;

}

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
    }

The slotReboot() is the slot that will receive the signal of the QAction I'm going to show in the mainwindow.cpp

mainwindow.cpp

First initialize EXIT_CODE_REBOOT :

int const MainWindow::EXIT_CODE_REBOOT = -123456789;

and declare a QAction pointer:

QAction* actionReboot;

then in the MainWindow constructor:

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()));
}

And finally you need to send the signal (in the part of your code you need), in this way:

actionReboot->trigger();

I did the code I showed following these instructions: How to make an application restartable - Qt Wiki

一张白纸 2024-10-26 12:40:03

要重新启动应用程序,请尝试:

#include <QApplication>
#include <QProcess>

...

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

To restart application, try:

#include <QApplication>
#include <QProcess>

...

// restart:
qApp->quit();
QProcess::startDetached(qApp->arguments()[0], qApp->arguments());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文