欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

QT启动进程

程序员文章站 2022-05-10 09:39:33
...

#include “widget.h”
#include “ui_widget.h”
#include
#include

Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);

}

Widget::~Widget()
{
delete ui;
}

void Widget::on_pushButton_clicked()
{
QProcess* process = new QProcess(this);//创建进程对象
connect(process,SIGNAL(stateChanged(QProcess::ProcessState)),
this,SLOT(SLOT_stateChanged(QProcess::ProcessState)));//当状态发生改变时
connect(process,SIGNAL(started()),this,SLOT(SLOT_started()));//进入"运行状态"
connect(process,SIGNAL(finished(int,QProcess::ExitStatus)),
this,SLOT(SLOT_finished(int,QProcess::ExitStatus)));//进程退出

process->start("ls");//启动ls

}

void Widget::SLOT_stateChanged(QProcess::ProcessState state)
{
switch (state) {
case QProcess::Starting:
qDebug() << “正在启动”;
break;
case QProcess::Running:
qDebug() << “正在运行”;
case QProcess::NotRunning:
qDebug() << “停止”;
break;
default:
break;
}
}

void Widget::SLOT_started()
{
qDebug() << process->readAll().data();
}

void Widget::SLOT_finished(int iExit, QProcess::ExitStatus finished)
{
qDebug() << iExit;
switch (finished) {
case QProcess::NormalExit:
qDebug() << “QProcess::NormalExit正常退出”;
break;
case QProcess::CrashExit:
qDebug() << “QProcess::CrashExit进程崩溃”;
break;
default:
break;
}
}

相关标签: qt