gpt4 book ai didi

c++ - qt 定期检查 QProcess 状态

转载 作者:太空宇宙 更新时间:2023-11-04 15:34:44 24 4
gpt4 key购买 nike

我从一个文本文件启动多个 QProcesses(例如像 notepad.exe 等不同的程序)并想定期检查它们是否仍在运行。

while 循环不是解决方案,因为它阻塞了 ui 线程。如何实现?在 Java (Android) 中,我会用一个 asynctask 来做到这一点连接到 ScheduldExecutorService。 qt 有类似的东西吗?

这是我开始流程的地方:

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

这里调用的方法是:

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

最佳答案

每个 QProcess 都有 finished(int exitCode, QProcess::ExitStatus exitStatus)stateChanged(QProcess::ProcessState newState) 信号,即当某些进程已终止或更改(按类型)时发出。所以你的代码可以是:

.H 面:

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

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

评论问题的更新

你可以这样试试:

.H面

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

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

关于c++ - qt 定期检查 QProcess 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37341867/

24 4 0
文章推荐: c++ - 内存重新分配未完全正常工作(节点)
文章推荐: html - 查找没有 alt =".#"的 标签的正则表达式
文章推荐: html - 样式化