qt中如何运行外部程序,QProcess类
1个回答
展开全部
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");
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询