QProcess stateChanged 信号未正确发出

问题描述

我想编写一个简单的应用程序(即启动器)来启动我的主应用程序并进一步监视其状态并在崩溃/退出时重新启动它。

很简单。为此,我使用 QProcess 并运行主应用程序 (app1) 并处理其 stateChanged 信号。

为了测试它,我编写了一个非常小的控制台应用程序,它会打印出它的名称,然后通过 exit(-1) 返回(模拟程序由于某种原因退出的情况)。

第一次一切都按预期进行,并且在每个阶段都发出 stateChanged 信号。 App1 会在启动后立即退出,再次导致 staeChanged 信号第二次发出。

问题是在第二次启动 APP1(并因此退出)后,不再发出 stateChanged 信号!!!

我做错了什么?每次我通过 QProcess 或 ...?

启动应用程序时,我是否应该重新连接信号

注意:我在 windows 和 linux 下都看到这种情况。

starter::starter(QObject *parent) : QObject(parent)
{
    m_process = new QProcess(this);
    m_process->setProcessChannelMode(QProcess::ProcessChannelMode::ForwardedChannels);
    connect(m_process,&QProcess::stateChanged,this,&starter::onStateChanged);

    m_process->setProgram("myapp1");
    m_process->setArguments(QStringList());

    startProcess();
}

void starter::onStateChanged(const QProcess::Processstate &state)
{
    qDebug() << "state changed to : " << state;

    if(state == QProcess::Processstate::NotRunning){
        this->startProcess();
    }
}


void starter::startProcess()
{
    qDebug() << "start process App1";
    m_process->start();
    if(m_process->waitForStarted(1000)){
        qDebug() << "[+] App started";
    }
    else{
        qDebug() << "[!] Failed to start app";
    }
}

输出

C:\myapp1-release>starter.exe
start process : "myapp1"
state changed to :  QProcess::Starting
state changed to :  QProcess::Running
[+] App started
state changed to :  QProcess::NotRunning
start process : "myapp1"
state changed to :  QProcess::Starting
state changed to :  QProcess::Running
[+] App started

*NO FURTHER REACTION TO THE EXIT OF APP1*

解决方法

我的(不太好)解决方案: 当我检测到应用程序崩溃/退出时,我删除旧的 QProcess 对象并再次实例化它。

通过这种方式,所有其他和随后的应用程序崩溃/退出都会发出信号。

m_process->deleteLater();//delete the old instance - I hope its signal/slot connections are cleared automatically by Qt ??!!
m_process = new QProcess(this);
m_process->setProcessChannelMode(QProcess::ProcessChannelMode::ForwardedChannels);
connect(m_process,&QProcess::stateChanged,this,&starter::onStateChanged);//make connections for the new instance

采用这种方法的提示来自 Qt 的 QProcess::close() 文档:

关闭与进程的所有通信并杀死它。 之后 调用此函数,QProcess 将不再发出 readyRead(),并且 无法再读取或写入数据。

Qt 的文档是否没有提到如果 qprocess 应用程序退出或被手动杀死,将不会再发出 stateChanged 信号?或者可能我错过了某事。

对于这种方法的正确性的任何评论都非常感谢。