• 12367阅读
  • 3回复

qprocess线程调用问题 [复制链接]

上一主题 下一主题
离线hustwsp
 
只看楼主 正序阅读 楼主  发表于: 2009-11-10
— 本帖被 XChinux 执行加亮操作(2010-09-03) —
void MainWindow::on_pushButton_clicked()
{
QProcess p;
QString file=QCoreApplication::applicationDirPath();//获取当前应用程序路径
file=file +"/test.bat";
p.execute(file);          //case1
//p.startDetached(file);     //case2
}

我想通过QProcess实现外部程序的调用,我这里调用个批处理程序为例子:
1)当我利用case1的execute时候可以正常调用外部程序,但是当我关闭外部程序的时候,QT主程序出现无法响应的情况;
2)但我利用case2的startDetached调用外部程序的时候,同时出现了2个外部程序,但是关闭外部程序的时候,QT主程序正常;

有没有高手帮我解决下这个问题,另外能不能解释下execute和tartDetached的区别,本人刚接触QT,谢谢赐教。
离线atlantis327

只看该作者 3楼 发表于: 2010-08-20
int QProcess::execute ( const QString & program )   [static]
Starts the program program with the arguments arguments in a new process, waits for it to finish, and then returns the exit code of the process.

bool QProcess::startDetached ( const QString & program )   [static]
Starts the program program with the arguments arguments in a new process, and detaches from it. Returns true on success; otherwise returns false. If the calling process exits, the detached process will continue to live.

函数说明说得很清楚。
离线haulm

只看该作者 2楼 发表于: 2009-11-12
QProcess 运行的子进程有可能会运行多次,你安插一个计数器就可以发现了,它产生的是一个完完全全的子进程,子进程和父进程是不同步的,所以QProcess只好用做简单的应用。或者也有朋友对QProcess类有更多的了解和应用,可我想还不如写一个线程得了。
离线haulm

只看该作者 1楼 发表于: 2009-11-12
最好不要用QProcess去运行UI程序,它只适合运行一些脚本等运行完毕就退出的程序。
而且多线程和多进程我都不使用QProcess,因为它只适用于简单的调用,运行多个QProcess则不要指望子进程能按顺序返回值。
在Linux下直接用system或popen等C函数来调用外部程序,win上面的我不懂,QProcess不能应用于复杂的外部调用。
快速回复
限100 字节
 
上一个 下一个