❶ qt中如何运行外部程序,QProcess类
QT中使用QProcess启用外部程序 启用外部程序,并可传参,默认第一个参数是exe路径!启动外部程序的方法有以下两种: 1、start()void QProcess::start ( const QString & program, const QStringList & arguments, OpenMode mode = ReadWrite ) Starts the program program in a new process, passing the command line arguments in arguments. The OpenMode is set to mode. QProcess will immediately enter the Starting state. If the process starts successfully,QProcess will emit started(); otherwise, error() will be emitted.Note that arguments that contain spaces are not passed to the process as separate arguments.Windows: Arguments that contain spaces are wrapped in quotes.Note: Processes are started asynchronously, which means the started() and error() signals may be delayed. Call waitForStarted() to make sure the process has started (or has failed to start) and those signals have been emitted.See also pid(), started(), and waitForStarted(). 2、使用QProcess::execute(), 不过使用此方法时程序会最阻塞直到此方法执行的程序结束后返回,这时候可使用QProcess和QThread这两个类结合使用的方法来处理,以防止在主 线程中调用而导致阻塞的情况 先从QThread继承一个类,重新实现run()函数: 答:1、使用QProcess::startDetached()方法,启动外部程序后立即返回; 2、使用QProcess::execute(),不过使用此方法时程序会最阻塞直到此方法执行的程序结束后返回,这时候可使用QProcess和QThread这两个类结合使用的方法来处理,以防止在主线程中调用而导致阻塞的情况 先从QThread继承一个类,重新实现run()函数:Quote:class MyThread : public QThread{public:void run(); };void MyThread::run(){QProcess::execute("notepad.exe");
❷ Qt 之启动putty,winscp
简述
QProcess可以用来启动外部程序,并与它们交互。
要启动一个进程,通过调用start()来进行,参数包含程序的名称和命源激令行参数,参数作为一个QStringList的单个字符串。
另外,也可以使用setProgram()和setArguments()来运行,然后调用start()或open()。
接口———————————谨册—————
start() 启动外部程序
readAllStandardError() 从标准错误祥裂宏中获取所有数据
readAllStandardOutput() 从标准输出中获取所有数据
write() 继承于QIODevice
close() 继承于QIODevice
除此之外,QProcess还包含静态成员函数:
execute() 启动一个进程,然后等待该进程结束。
startDetached() 启动一个进程,然后使其和当前进程脱离进程的父子关系。
❸ qt调用外部exe程序并且将exe程序一起打包
你试试作为一个资源来运行看行不行,或者将exe作为一个资源,然后运行回时以二进制的形式读取,再答将读取到的二进制信息写入到临时文件中,运行临时文件。或者直接将资源中得exe保存到系统临时文件夹中进行运行。
只是个思路,没试过,你试试
❹ QT调用外部程序打开聊天窗口
// 设置一个高频定时器,移动当前窗口位置,然后又移动回来,来回手早交替就可以败樱了!~QTimer *timer = new QTimer(this);timer->start(10);connect(timer, SIGNAL(timeout()), this, SLOT(sltTimeout()));void MainWindow::sltTimeout() { static int nX = this->rect().x(); static int nY = this->rect().y(); if (nX == this->rect().x()) this->毕枯雀move(nX-10, nY -10); else this->move(nX, nY);}
❺ QT 启动与关闭外部程序
启动
QString filepath = QDir::currentPath();
QString fullpath = filepath+"\\pp\\dist\\start";
QString title = fullpath+"\\main.exe";
title = title.replace("/","\\");
fullpath = fullpath.replace("/","\\");
袜答 QProcess p(this);
p.setWorkingDirectory(fullpath);
p.start("cmd", QStringList()<<"/c"<<"start main.exe");
p.waitForStarted();
p.waitForFinished();
关闭
QProcess p(this);
p.setWorkingDirectory(fullpath);
庆洞 p.start("告差慧cmd", QStringList()<<"/c"<<"taskkill /f /t /im main.exe");
p.waitForStarted();
p.waitForFinished();
❻ Qt调用外部程序的问题
先看看返回值是多少?灶丛灶如隐扮果调用成功,使用
startDetached(const QString& program, const QStringList & arguments, const QString & workingDirectory, qint64 * pid = 0 ) 函数,看看pid是郑敬多少。
❼ Qt打开外部程序(界面与程序连接)
#include<Qprocess>
void MainWindow2::program() //打开外部程序
{
this->hide(); /枯凯/隐藏型薯当前界面
QProcess::execute("……\\xx.exe"); //外部没租唤程序所在路径
}
❽ QT调用外部程序
1、通过调用Linux C 函数
system("calc.exe");
2、通过碧梁QProcess 阻塞调用
QProcess::execute("calc.exe");/QProcess::startDetached("calc.exe");
3、通过QProcess,非阻塞调用
QProcess *pro = new QProcess;
pro->start("calc.exe");
注释:
1、前两种方法会阻塞进程,直到计算器程序结束,而第三种方法则不会阻塞进程,可以多任务运行。
2、QT在运行的时候,要启动qws服务尺虚,如果用前两种方法,运行的时候,要新开启一个qws,否则不能运行;而第三种方法,则不需要在开启qws,他和主进程公用一个qws。
3、第三种虽然不会阻塞,但是有可能在终端上看不到打印出来的信息。所以要在终端显示信息,可以考陵慧燃虑阻塞模式。
❾ 将外部程序嵌入qt
网上搜了下实现方案,原理很简单,就是通过获取外部窗口的id,转换为QWindow嵌入当前qt窗口氏宴唯。于是问题就变成了如何查看外部程序的窗口属性,推荐使用spy++,小巧实用,放链接:
链接:https://pan..com/s/15cZuL1OPITdM1OwnTkBSww
以记事本程序为例,可以祥薯看到窗口标题为"无标题-歼培记事本",类型为Notepad。
❿ Qt QProcess
本文介绍,在Linux环境下,使用Qt中的 QProcess 类执行shell命令并获取输出。
头文件: #include <QProcess>
一、函数接口
QProcess 类提供了三个函数
二、执行命令
这里主要介绍 execute() 和 start() :
execute() 会将命令输出直接打印到控制台,调用程序无法捕获。
调用程序可通过 readAllStandardOutput() 捕获shell命令的输出
三、管 道
对于shell命令中的 | ,直接传入参数是不行的。
以上的方式是无法执行的。
可以将整个命令作为 sh 的参数传入 或 使用 QProcess::setStandardOutputProcess(QProcess *destination) 即将一个进程的标准输出流传入目标进程的标准输入流
对于需要sudo权限的命令,需要使用sudo权限打开qtcreator,或者直接在命令前加上sudo(不建议)。
当然, QProcess 不仅仅可以执雀厅行shell命令旅早,也可以用于执行调拆岁雀用外部程序。