根据QT帮助文档,写了一个小测试案例,在QT应用中,启动 一个外部的exe程序,目前此exe使用的QT编译形成,后期改用C#形式的exe文件。
#include "QProcessDemo.h"
#include <QPushButton>
#include <QProcess>
#include <QMessageBox>
#include <QDir>
QProcessDemo::QProcessDemo(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
connect(ui.startBtn, SIGNAL(clicked(void)), this, SLOT(startNewExe(void)));
connect(ui.testBtn, SIGNAL(clicked(void)), this, SLOT(testBtn(void)));
}
QProcessDemo::~QProcessDemo()
{
delete[] myProcess;
}
void QProcessDemo::startNewExe(void)
{
QString path = QCoreApplication::applicationFilePath();
QMessageBox::information(this, QStringLiteral("测试启动exe"), path);
//方案1
//启动新的exe运行, 路径中不能有空格,
// QString program = QStringLiteral("F:/fabu/01 发布demo测试/ReleaseApp/ReleaseApp.exe"); //此路径就有问题
QString program = QStringLiteral("D:/ReleaseApp/ReleaseApp.exe");
myProcess = new QProcess(this);
myProcess->start(program);
#if 0
//方案2,此方式,会造成阻塞
QProcess process(this);
process.start("D:/ReleaseApp/ReleaseApp.exe");
process.start();
process.waitForStarted(); //等待程序启动
process.waitForFinished();//等待程序关闭
#endif
}
void QProcessDemo::testBtn(void)
{
QMessageBox::information(this, QStringLiteral("测试原app"), QStringLiteral("原app正常运行"));
}
## 带参数调用exe
QProcess process(this);
QStringList arguments;
arguments << domain_pddl << problem_pddl;
process.start(smtplan_exe, arguments);
process.waitForFinished();
QByteArray qByteRead = process.readAllStandardOutput();
QString tmpStr = QString::fromLocal8Bit(qByteRead);