本文介绍了Qt避免警告“QProcess:销毁,而进程仍在运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最简单的代码:

void test
{
    QProcess p;
    p.start("sleep 10");
    p.waitForBytesWritten();
    p.waitForFinished(1);
}

当然,该过程不能在函数结束之前完成,因此它显示一条警告消息:

Of course, the process can't be finished before the end of the function, so it displays a warning message:

 QProcess: Destroyed while process ("sleep") is still running.

我希望此消息不会显示 - 我应该在结束功能之前自己销毁该进程,但我找不到如何正确地这样做:p。〜QProcess(),p.terminate(),p.kill()不能帮助我。

I want this message not to be shown - I should destroy the process by myself before end of function, but I can't find how to do this correctly: p.~QProcess(), p.terminate(), p.kill() can't help me.

推荐答案

您可以根据你的愿望明确地杀死或终止进程。但是,它本身不够,因为你实际上需要等待进程终止。 kill意味着它将在Unix上发送SIGKILL信号给进程,并且也需要一些时间来实际完成。

You can kill or terminate the process explicitly depending on your desire. That is, however, not enough on its own because you actually need to wait for the process to terminate. "kill" means it will send the SIGKILL signal on Unix to the process and that also takes a bit of time to actually finish.

因此,你会写this:

Therefore, you would be writing something like this:

#include <QProcess>

int main()
{
    QProcess p;
    p.start("sleep 10");
    p.waitForBytesWritten();
    if (!p.waitForFinished(1)) {
        p.kill();
        p.waitForFinished(1);
    }
    return 0;
}



main.pro



main.pro

TEMPLATE = app
TARGET = main
QT = core
SOURCES += main.cpp

这篇关于Qt避免警告“QProcess:销毁,而进程仍在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 23:38