我想从我的QT-Programm中启动一个外部程序。唯一有效的解决方案是:
system("start explorer.exe");
但它只适用于windows,并且会启动一段时间的命令行。
我尝试的下一步是:
QProcess process;
QString file = QDir::homepath + "file.exe";
process.start(file);
//process.execute(file); //i tried as well
但什么都没发生。有什么想法吗?
发布于 2013-10-18 14:20:11
如果您的process
对象是堆栈上的一个变量(例如,在一个方法中),代码将不会按预期工作,因为当方法结束时,您已经启动的进程将在QProcess
的析构函数中终止。
void MyClass::myMethod()
{
QProcess process;
QString file = QDir::homepath + "file.exe";
process.start(file);
}
相反,您应该像这样在堆上分配QProcess
对象:
QProcess *process = new QProcess(this);
QString file = QDir::homepath + "/file.exe";
process->start(file);
发布于 2015-11-17 23:45:43
如果您希望程序在进程执行期间等待,并且只需要获取其退出代码,则可以使用
QProcess::execute(file);
QProcess::exitCode(); // returns the exit code
而不是像这样异步使用进程。
QProcess process;
process.start(file);
请注意,您还可以阻止执行,直到进程结束。为了做到这一点
process.waitForFinished();
在该过程开始之后。
发布于 2013-10-18 14:00:14
QDir::homePath不以分隔符结尾。到您的exe的有效路径
QString file = QDir::homePath + QDir::separator + "file.exe";
https://stackoverflow.com/questions/19442400
复制相似问题