C++ qt定期检查QProcess状态
我正在从一个文本文件(例如notepad.exe等不同程序)启动多个QProcess,并希望定期检查它们是否仍在运行 while循环不是解决方案,因为它阻塞了ui线程。 如何做到这一点?在Java(Android)中,我会使用异步任务来实现这一点 已连接到ScheduledExecutor服务。qt是否有类似的功能 这是我开始我的流程的地方:C++ qt定期检查QProcess状态,c++,qt,C++,Qt,我正在从一个文本文件(例如notepad.exe等不同程序)启动多个QProcess,并希望定期检查它们是否仍在运行 while循环不是解决方案,因为它阻塞了ui线程。 如何做到这一点?在Java(Android)中,我会使用异步任务来实现这一点 已连接到ScheduledExecutor服务。qt是否有类似的功能 这是我开始我的流程的地方: void mywidget::startprocesses(QString &text) { QProcess *process = ne
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);
}
}
while循环不是解决方案,因为它阻塞了ui线程
对。但是,由于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
//假设我们有一个名为timerList的进程列表QList
QTimer*pTimer=新的QTimer;
连接(pTimer,&QTimer::timeout,[=](){
foreach(QProcess*proc,timerList)
{
//获取状态
int state=进程->状态();
//更新用户界面
addlabelstatus(州);
}
});
p定时器->开始(1000);//每一秒
或者,正如框中的@someoneinthebox所回答的,连接到每个QProcess的信号,在它发生时通知您并对此作出反应
while循环不是解决方案,因为它阻塞了ui线程
对。但是,由于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
//假设我们有一个名为timerList的进程列表QList
QTimer*pTimer=新的QTimer;
连接(pTimer,&QTimer::timeout,[=](){
foreach(QProcess*proc,timerList)
{
//获取状态
int state=进程->状态();
//更新用户界面
addlabelstatus(州);
}
});
p定时器->开始(1000);//每一秒
或者,正如框中的@someone所回答的,连接到每个QProcess的信号,以在它发生时通知您并对其作出反应。每个
QProcess
都已完成(int-exitCode,QProcess::ExitStatus ExitStatus)
和状态更改(QProcess::ProcessState newState)
信号,当某个进程被终止或更改(按类型)时发出的。因此,您的代码可以是:
.H侧:
public slots:
void processFinished(int exitCode, QProcess::ExitStatus exitStatus);
void addlabelstatus(QProcess::ProcessState newState);
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);
.CPP侧:
public slots:
void processFinished(int exitCode, QProcess::ExitStatus exitStatus);
void addlabelstatus(QProcess::ProcessState newState);
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);
.CPP侧
public slots:
void processFinished(QLabel *label, int exitCode, QProcess::ExitStatus exitStatus);
void addlabelstatus(QLabel *label, QProcess::ProcessState newState);
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);
}
}
void mywidget::processFinished(QLabel*标签、int-exitCode、QProcess::ExitStatus ExitStatus)
{
}
void mywidget::addlabelstatus(QLabel*label,QProcess::ProcessState newState)
{
交换机(新闻状态){
};
}
而(!stream->atEnd()){
此->文件读取=流->读取行();
如果(!this->fileread.isEmpty()){
QLabel*label=新的QLabel(此->文件读取);
QProcess*process=新的QProcess(此);
此->myprocess.append(进程);
连接(进程,&QProcess::finished,[=]
(int exitCode,QProcess::ExitStatus ExitStatus)
{processFinished(标签、exitCode、exitStatus);};
连接(进程,&QProcess::stateChanged,[=]
(QProcess::ProcessState新闻状态)
{addlabelstatus(标签,新闻状态);};
进程->开始(文本);
}
}
每个QProcess
都已完成(int-exitCode,QProcess::ExitStatus-ExitStatus)
和stateChanged(QProcess::ProcessState-newState)
信号,当某些进程已终止或更改(按类型)时发出。因此,您的代码可以是:
.H侧:
public slots:
void processFinished(int exitCode, QProcess::ExitStatus exitStatus);
void addlabelstatus(QProcess::ProcessState newState);
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);
.CPP侧:
public slots:
void processFinished(int exitCode, QProcess::ExitStatus exitStatus);
void addlabelstatus(QProcess::ProcessState newState);
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);
.CPP侧
public slots:
void processFinished(QLabel *label, int exitCode, QProcess::ExitStatus exitStatus);
void addlabelstatus(QLabel *label, QProcess::ProcessState newState);
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);
}
}
void mywidget::processFinished(QLabel*标签、int-exitCode、QProcess::ExitStatus ExitStatus)
{
}
void mywidget::addlabelstatus(QLabel*label,QProcess::ProcessState newState)
{
交换机(新闻状态){
};
}
而(!stream->atEnd()){
此->文件读取=流->读取行();
如果(!this->fileread.isEmpty()){
QLabel*label=新的QLabel(此->文件读取);
QProcess*process=新的QProcess(此);
此->myprocess.append(进程);
连接(进程,&QProcess::finished,[=]
(int exitCode,QProcess::ExitStatus ExitStatus)
{processFinished(标签、exitCode、exitStatus);};
连接(进程,&QProcess::stateChanged,[=]
(QProcess::ProcessState新闻状态)
{addlabelstatus(标签,新闻状态);};
进程->开始(文本);
}
}
?很不错的。它几乎可以完美地工作:-)一件事,在我的addlabelstatus中,我创建了一个动态包含流程描述的QLabel。因此,现在每次更新流程时,都会创建一个新标签。如何在每次状态更新occours时删除这些动态添加的QLabel?我在最初的线程中添加了addlabelstatus方法,但为什么要这样做?一次创建一个QLabel
,并通过它的对象名或指针来更新它。所以一开始我不知道创建了多少个进程。非常好。它几乎可以完美地工作:-)一件事,在我的addlabelstatus中,我创建了一个动态包含流程描述的QLabel。因此,现在每次更新流程时,都会创建一个新标签。如何在每次状态更新occours时删除这些动态添加的QLabel?我在最初的线程中添加了addlabelstatus方法,但为什么要这样做?一次创建一个QLabel
,并通过它的对象名或指针来更新它。所以一开始我不知道创建了多少个进程。