Qt执行外部程序 [英] Qt Execute external program
问题描述
我想从我的QT-Programm中启动一个外部程序.唯一可行的解决方案是:
I want to start an external program out of my QT-Programm. The only working solution was:
system("start explorer.exe");
但是它仅适用于Windows并暂时启动命令行.
But it is only working for windows and starts a command line for a moment.
我尝试的下一件事情是:
Next thing I tried was:
QProcess process;
QString file = QDir::homepath + "file.exe";
process.start(file);
//process.execute(file); //i tried as well
但是什么也没发生.有什么想法吗?
But nothing happened. Any ideas?
推荐答案
如果您的process
对象是堆栈上的变量(例如,在方法中),则该代码将无法正常工作,因为您已经完成了该过程该方法完成后,已经开始的操作将在QProcess
的析构函数中被杀死.
If your process
object is a variable on the stack (e.g. in a method), the code wouldn't work as expected because the process you've already started will be killed in the destructor of QProcess
, when the method finishes.
void MyClass::myMethod()
{
QProcess process;
QString file = QDir::homepath + "file.exe";
process.start(file);
}
您应该在堆上分配 QProcess
对象
You should instead allocate the QProcess
object on the heap like that:
QProcess *process = new QProcess(this);
QString file = QDir::homepath + "/file.exe";
process->start(file);
这篇关于Qt执行外部程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!