问题
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