Qt中使用QProcess启动外部程序和关闭外部程序
2018-03-30 12:31
1671 查看
因为目前的程序需要提供一个文件对比的功能,而目前已经有专门的文本对比软件,所以我打算直接调用外部的文本对比程序。通过查阅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.
4000
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.
2、使用QProcess::execute(), 不过使用此方法时程序会最阻塞直到此方法执行的程序结束后返回,这时候可使用QProcess和QThread这两个类结合使用的方法来处理,以防止在主 线程中调用而导致阻塞的情况 先从QThread继承一个类,重新实现run()函数:
class ...............
{...........
MyThread thread;
............
};
.....................
thread.start();
在主程序退出时,启动的外部程序是不会随着主程序的退出而退出的,我们当然不希望这种情况。
继续查阅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. 可以看到,调用这个后会关闭所有的process启动的外部程序。因此,可以在主程序推出前,加一个判断
[cpp] view plain copyif(process)
process->close();
delete process;
process = 0;
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.
4000
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.
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();
在主程序退出时,启动的外部程序是不会随着主程序的退出而退出的,我们当然不希望这种情况。
继续查阅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. 可以看到,调用这个后会关闭所有的process启动的外部程序。因此,可以在主程序推出前,加一个判断
[cpp] view plain copyif(process)
process->close();
delete process;
process = 0;
相关文章推荐
- QT中使用QProcess启用外部程序和关闭外部程序
- QT中使用QProcess启用外部程序和关闭外部程序
- QT 使用QProcess 调用外部程序并截取输出流
- Qt之启动外部程序(使用参数很全面,还使用了setProcessChannelMode)
- QT中使用QProcess启用外部程序
- QT 使用QProcess 调用外部程序并截取输出流
- Qt之启动外部程序(使用参数很全面,还使用了setProcessChannelMode)
- Qt调用外部程序,启动并从中获取信息——QProcess
- Qt之启动外部程序(使用参数很全面,还使用了setProcessChannelMode)
- 使用Qt打开外部文件,调用外部程序(QDesktopServices,QUrl ,QProcess,QProcess)
- 如何使用java程序界面上的按钮启动外部的exe程序
- QT中调用外部程序:QProcess的使用
- C#启动外部程序的几种方法以及等待外部程序关闭的方法(转)
- QT使用GIF作程序启动画面
- QT程序启动界面的使用
- 重新想象 Windows 8 Store Apps (33) - 关联启动: 使用外部程序打开一个文件或uri, 关联指定的文件类型或协议
- QT程序启动界面的使用
- 使用Qt编辑关闭窗口程序的一些见解
- QT中调用外部程序:QProcess的使用
- 使用Qt编辑关闭窗口程序的一些见解