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
需要通过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.