Qt avoid warning 'QProcess: destroyed while process still running

与世无争的帅哥 提交于 2019-12-10 15:44:34

问题


Simplest code:

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.

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.

NOTE: I don't want wait for process execution, just kill it when its running, by myself.


回答1:


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.

Therefore, you would be writing something like this:

main.cpp

#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

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


来源:https://stackoverflow.com/questions/23286001/qt-avoid-warning-qprocess-destroyed-while-process-still-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!