首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Qt中杀死一个QProcess

问在Qt中杀死一个QProcess
EN

Stack Overflow用户
提问于 2015-04-07 12:11:39
回答 1查看 6.4K关注 0票数 0

我想知道如何在Qt,Linux中杀死一个QProcess。我启动了一个外部应用程序,比如一个显示图像的应用程序(全屏),没有标题栏,所以没有关闭按钮。现在我需要关闭这个应用程序,比如说在5秒之后。我怎么才能在Qt里做到这一点?

代码语言:javascript
复制
void MainWindow::Dialog()
{
  ......
  ......
  connect(pbLaunchImage,SIGNAL(released()),this, SLOT(launchImage()));
  //I am guessing a connect here to kill the started process
}

void MainWindow::launchImage()
{
    qWarning("Launching Image...");
    p->execute("/usr/bin/fancyImage");
}

编辑:

我仍然没有弄清楚,但我找到了一个解决办法,因为我有外部应用程序的来源。我添加了一个退出操作,关闭打开的外部应用程序。但是,如果有人对QProcess有任何了解,请将其作为答案发布。

EN

回答 1

Stack Overflow用户

发布于 2015-04-07 12:17:02

首先,您不能使用QProcess::execute(...),因为它是一种方便阻塞的方法。您的Qt程序有效地停止了(如果它是一个GUI程序,并且它停止不止一次眨眼)。您需要使用QProcess::start(...)启动该进程,并让您的Qt程序继续运行。

如果子进程行为良好,只需调用QProcess::terminte()槽方法使其退出即可。子进程被发送术语信号(在Linux/Unix上),然后它应该干净地退出。

如果进程行为不佳,并且拒绝定期退出,则必须使用QProcess::kill()槽方法,但如果可能的话,我更希望修复子应用程序。

然后,要使它在5秒后退出,请使用QTimer。所以,在您创建p的地方,假设它是一个QProcess,而不是其他东西:

代码语言:javascript
复制
// p = new QProcess... or something equivalent happened here
// below code needs to be done once per creation of `p`

// create a timer with p as parent, so it will be deleted properly
QTimer *killtimer = new QTimer(p); 
killtimer->setTimeout(5000); // 5 seconds
killtimer->setSingleShot(true); // should happen only once per process start

// make the timer's timeout to terminate the process
QObject::connect(killtimer, SIGNAL(timeout()), p, SLOT(terminate()));

// make the timer start when process starts
QObject::connect(p, SIGNAL(started()), killtimer, SLOT(start()));

// you also want to connect signals to slots, and at least do debug print
// example:
QObject::connect(p, SIGNAL(started()), this, SLOT(handleStarted()));
QObject::connect(p, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(handleFinished(int,QProcess::ExitStatus)));
QObject::connect(p, SIGNAL(error(QProcess::ProcessError)), this, SLOT(handleError(QProcess::ProcessError)));

// then just launch the process. this program will also keep running
p->start("/usr/bin/fancyImage");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29491285

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档