QT中使用QProcess启用外部程序和关闭外部程序

因为目前的程序需要提供一个文件对比的功能,而目前已经有专门的文本对比软件,所以我打算直接调用外部的文本对比程序。
通过查阅QT的帮助文档,发现了QProcess这个类可以提供这种需求。
我找到的启动外部程序的方法有以下两种:
1、start()
void QProcess::start ( const  &  program, const  &  arguments,   mode = ReadWrite )

Starts the program program in a new process, passing the command line arguments in arguments. The  is set to mode.  will immediately enter the Starting state. If the process starts successfully, will emit (); otherwise, () 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 () and () signals may be delayed. Call () to make sure the process has started (or has failed to start) and those signals have been emitted.

See also (), (), and ().

2、使用QProcess::execute(), 不过使用此方法时程序会最阻塞直到此方法执行的程序结束后返回,这时候可使用QProcess和QThread这两个类结合使用的方法来处理,以防止在主 线程中调用而导致阻塞的情况

先从QThread继承一个类,重新实现run()函数:

Quote:
class MyThread : public QThread
{
public:
void run();
};
void MyThread::run()
{
QProcess::execute("notepad.exe");
}
这 样,在使用的时候则可定义一个MyThread类型的成员变量,使用时调用其start()方法:

Quote:
class ...............
{...........
MyThread thread;
............
};
.....................
thread.start();
两个的具体的区别我不是很清楚,但我在程序中分别试了下,发现第二个会阻塞主程序,但使用start()则不会。

下面是我使用QProcess启动WinMerge的代码。

#include <QProcess>
QProcess *process = new QProcess;
QStringList str;
str << "";
process->start("./WinMerge/WinMergeU.exe",str);
如果程序里面不输入参数,就会直接到软件直接运行时的界面。
加参数后弹出的界面是工具栏第一列中的第一个需要输入的界面(这个是我猜测的,不确定,但确实能弹出)。
下面是截图:
GUI里的按钮:


点击交换机配置比较后:



嗯,这样就可以启动外部程序了。

^-^补充:在主程序退出时,启动的外部程序是不会随着主程序的退出而退出的,我们当然不希望这种情况。
继续查阅QT帮助文档,发现close这个函数,看下它的说明:
void QProcess::close ()   [virtual]

Closes all communication with the process and kills it. After calling this function,  will no longer emit (), and data can no longer be read or written.

Reimplemented from .

可以看到,调用这个后会关闭所有的process启动的外部程序。因此,可以在主程序推出前,加一个判断

if(process) 
process->close();
delete process;
process = 0;






### QProcess 类在 Qt 框架中的位置 `QProcess` 是 `Qt Core` 模块的一部分,因此为了使用该类,需要引入相应的头文件: ```cpp #include <QProcess> ``` 此头文件包含了所有必要的定义来创建操作 `QProcess` 对象[^1]。 ### 创建与初始化 QProcess 实例 可以通过如下方式实例化 `QProcess`: ```cpp QProcess *process = new QProcess(parent); // 或者作为栈对象 QProcess process; ``` 这里 `parent` 参数指定了父对象,这有助于自动内存管理,在大多数情况下可以设置为当前窗口或其他合适的 QObject 子类的对象[^2]。 ### 启动外部程序的方法 要启动一个新的进程,可调用 `start()` 方法,并传递命令及其参数列表给它。例如,如果想要运行 Python 脚本,则可以用下面的方式做: ```cpp QString program = "python"; QStringList arguments; arguments << "-u" << "/path/to/script.py"; process->start(program, arguments); // 开始执行Python脚本 if (!process->waitForStarted()) { qDebug() << "Failed to start the script."; } ``` 上述代码片段展示了如何通过指定解释器 (`python`) 路径到目标 `.py` 文件来启动 Python 脚本。选项 `-u` 表明启用未缓冲的标准输入/输出流[^3]。 ### 进程间的通信机制 对于读取来自子进程的数据或向其发送数据而言,有几种方法可用: - **标准输出捕获**: 可以连接信号 `readyReadStandardOutput()` 到槽函数以便当有新数据到达时做出响应; - **写入标准输入**: 使用 `write()` 函数可以直接将字符串写入正在运行的进程中; 以下是简单的例子说明怎样监听并打印出由子进程产生的每一行文本: ```cpp connect(process, &QProcess::readyReadStandardOutput, [&]() { qDebug() << QString(process->readAllStandardOutput()); }); ``` 这段代码会每当接收到新的标准输出内容时就将其显示出来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值