简体   繁体   English

qt定期检查QProcess状态

[英]qt check QProcess state regularly

I´m starting multiple QProcesses from a textfile (for example different programs like notepad.exe and so on) and want to regularly check if they are still running. 我正在从一个文本文件(例如,诸如notepad.exe等不同的程序)启动多个QProcesses,并希望定期检查它们是否仍在运行。

A while loop isn´t the solution, since it is blocking the ui-thread. while循环不是解决方案,因为它阻塞了ui线程。 How can this be achieved? 如何做到这一点? In Java (Android) I would do this with a asynctask connected to a ScheduldExecutorService. 在Java(Android)中,我将使用连接到ScheduldExecutorService的asynctask来执行此操作。 Is something similar available for qt? Qt有类似的东西吗?

This is where I start my processes: 这是我开始流程的地方:

void mywidget::startprocesses(QString &text)
{
    QProcess *process = new QProcess(this);
    this->myprocess.append(process);
    process->start(text);
    int state = process->state();
    addlabelstatus(state);
}

And here the method is called: 在这里,该方法称为:

while(!stream->atEnd())                                    //check stream until empty and assign line as a caption to a new QLabel
        {
            this->fileread = stream->readLine();
            if(!this->fileread.isEmpty())
            {
                central->addlabel(this->fileread);
                central->startprocesses(this->fileread);
            }
        }






void mywidget::addlabelstatus(QProcess::ProcessState newstate)
{
    QString sstring;

    if(newstate == 0)
    {
        QString sstring = "Wird nicht ausgeführt";
        QLabel *label = new QLabel(sstring);
        this->processstatus.append(label);
        this->vrarea->addWidget(label);
    }
    else if (newstate == 1)
    {
        QString sstring = "Wird gestartet!";
        QLabel *label = new QLabel(sstring);
        this->processstatus.append(label);
        this->vrarea->addWidget(label);
    }
    else if (newstate == 2)
    {
        QString sstring = "Wird ausgeführt!";
        QLabel *label = new QLabel(sstring);
        this->processstatus.append(label);
        this->vrarea->addWidget(label);
    }
    else
    {
        QString sstring = "kein Status vorhanden!";
        QLabel *label = new QLabel(sstring);
        this->processstatus.append(label);
        this->vrarea->addWidget(label);
    }
}

Each QProcess has finished(int exitCode, QProcess::ExitStatus exitStatus) and stateChanged(QProcess::ProcessState newState) signals, that emits when some process has been terminated or changed (by type of). 每个QProcess都有finished(int exitCode, QProcess::ExitStatus exitStatus)stateChanged(QProcess::ProcessState newState)信号,它们在某些进程被终止或更改时(按类型)发出。 So your code can be: 因此,您的代码可以是:

.H side: .H面:

public slots:
    void processFinished(int exitCode, QProcess::ExitStatus exitStatus);
    void addlabelstatus(QProcess::ProcessState newState);

.CPP side: .CPP端:

void mywidget::processFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
    <...>
}

void mywidget::addlabelstatus(QProcess::ProcessState newState)
{
    switch(newState) {
        <...>
    };
}

// Your code change

QProcess *process = new QProcess(this);
this->myprocess.append(process);
connect(process, &QProcess::finished, this, &mywidget::processFinished);
connect(process, &QProcess::stateChanged, this, &mywidget::addlabelstatus);
process->start(text);

UPDATE FOR COMMENT QUESTION 更新评论问题

You can try this way: 您可以这样尝试:

.H side .H侧

public slots:
    void processFinished(QLabel *label, int exitCode, QProcess::ExitStatus exitStatus);
    void addlabelstatus(QLabel *label, QProcess::ProcessState newState);

.CPP side .CPP端

void mywidget::processFinished(QLabel *label, int exitCode, QProcess::ExitStatus exitStatus)
{
    <...>
}

void mywidget::addlabelstatus(QLabel *label, QProcess::ProcessState newState)
{
    switch(newState) {
        <...>
    };
}

while(!stream->atEnd()) {
            this->fileread = stream->readLine();
            if(!this->fileread.isEmpty()) {
                QLabel *label = new QLabel(this->fileread);
                QProcess *process = new QProcess(this);
                this->myprocess.append(process);

                connect(process, &QProcess::finished, [=] 
                (int exitCode, QProcess::ExitStatus exitStatus) 
                { processFinished(label, exitCode, exitStatus); });

                connect(process, &QProcess::stateChanged, [=] 
                (QProcess::ProcessState newState) 
                { addlabelstatus(label, newState); });

                process->start(text);
            }
}

A while loop isn´t the solution, since it is blocking the ui-thread. while循环不是解决方案,因为它阻塞了ui线程。

Correct. 正确。 However, since Qt is an event-driven framework, you could use a timer:- 但是,由于Qt是事件驱动的框架,因此可以使用计时器:

// Assuming we have a list of processes QList<QProcess*> called timerList

QTimer* pTimer = new QTimer;
connect(pTimer, &QTimer::timeout, [=](){

    foreach(QProcess* proc, timerList)
    {
        // get state
        int state = process->state();
        // update UI
        addlabelstatus(state);            
    }

});

pTimer->start(1000); // every second

Or alternatively, as @someoneinthebox answered, connect to each QProcess's stateChanged signal to notify you when it occurs and react to that. 或者,就像@someoneinthebox回答的那样,连接到每个QProcess的stateChanged信号,以在发生时通知您并对此做出反应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM