#include <QCoreApplication>
#include <QProcess>
#include <QDebug>
int main01(int argc, char** argv)
{
QCoreApplication app(argc, argv);
QProcess process;
process.start("tasklist");//从属主进程
process.waitForFinished();//进程同步执行
qDebug() << process.readAll();
qDebug() << process.exitCode();
return app.exec();
}
int main02(int argc, char** argv)
{
QCoreApplication app(argc, argv);
QProcess process;
process.startDetached("tasklist");//独立的进程
process.waitForFinished();//进程同步执行
qDebug() << process.readAll();
qDebug() << process.exitCode();
return app.exec();
}
int main03(int argc, char** argv)
{
QCoreApplication app(argc, argv);
QProcess process;
process.start("tasklist");//从属主进程
QObject::connect(&process,static_cast< void (QProcess:: *) (int,QProcess::ExitStatus)> (&QProcess::finished),[&](){
qDebug() << process.readAll();
qDebug() << process.exitCode();
});
qDebug() << "进程异步执行";
return app.exec();
}
注:1.pro文件中加入CONFIG += C++11
2.lambda表达式的应用,遇到重载的信号函数需要使用static_cast强制转换以区分