Qt 调用第三方应用程序

来源:互联网 发布:vb语言n的数值 编辑:程序博客网 时间:2024/05/19 22:26
Qt 调用第三方程序:


需要包含头文件<QProcess>

下面的三种方法中,前两种是阻塞方式,第三种是非阻塞方式执行。


1、简单情况,调用Windows系统的记事本程序:
Windows记事本的路径在系统环境变量中,不用加参数,所以调用是比较容易的:


{
QProcess *po = new QProcess(this);//如果不在类中,则参数为空
po->start("notepad.exe");
}


2、调用cmd并获取输出信息:
执行一个ping 命令:
{
QProcess p(0);
p.start("cmd",QStringList()<<"/c"<<"ping www.csdn.net");
p.waitForStarted();
p.waitForFinished();
QString strTemp=QString::fromLocal8Bit(p.readAllStandardOutput());


QMessageBox testMessage;
testMessage.setText(strTemp);
testMessage.exec();
}


3、执行带参数的位置不在环境变量路径中的程序:
假设程序路径为"d:\test\a.exe":
执行命令格式为:a.exe -i in.txt -j 1600 -file out.txt;
则代码为:
{
QProcess *po = new QProcess(this);
QString program="d:\test\a.exe";
QStringList argu;
argu.append("-i");
argu.append("in.txt");
argu.append("-j");
argu.append("1600");
argu.append("-file");
argu.append("out.txt");
po->start(program,argu);
}
原创粉丝点击