加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

QT运行外部批处理程序

(2012-08-19 14:31:24)
标签:

杂谈

分类: QT

QT运行外部.exe窗口程序可以直接调用QProcess的start("*.exe")函数,但在运行.bat批处理程序或控制台(console)应用程序时需通过QProcess调用cmd.exe,并用批处理文件或.exe控制台程序作为参数。具体代码如下:

 

QProcess p;
p.start("cmd.exe", QStringList() << "/c" << "c:\\uti\\mybat.bat");
if (p.waitForStarted()) 
{
   p.waitForFinished();
   qDebug() << p.readAllStandardOutput();
}
else
   qDebug() << "Failed to start";
对于控制台程序,也可通过下列代码指定其参数。
p.start("cmd.exe", QStringList() << "/C" << "myprog.exe" << str_arg1 
<< str_arg2 << ... << str_argn );
注意 "/C"和"myprog.exe"均为cmd.exe的参数。

另外,QProcess不能直接调用cmd.exe来显示控制台。

QProcess*p=new QProcess(this); 
p
->start("cmd.exe"); 

需要通过startDetached才能显示。

p->startDetached("cmd.exe"); 

 

详见 http://qt-project.org/doc/qt-4.8/qprocess.html

Notes for Windows Users

Some Windows commands (for example, dir) are not provided by separate applications, but by the command interpreter itself. If you attempt to use QProcess to execute these commands directly, it won't work. One possible solution is to execute the command interpreter itself (cmd.exe on some Windows systems), and ask the interpreter to execute the desired command.

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有