QT启动进程
#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;
}
}
上一篇: 判断当前用户是否为root