QProcess doesn't stop when the command is finished
-
Hi, I'm trying to get a live output out of a running thread, but I have a problem. When the command is supposed to finish, QProcess is still running. When I tried to run it without the live output, it worked fine.
Here's what I'm working with:QProcess fd; fd.setWorkingDirectory(dir + "platform-tools"); QStringList command; command << "fastboot devices"; fd.startDetached("powershell", command); while(fd.Running) { //without this while it works fine qDebug() << fd.readAll(); msleep(100); }
After the command is "finished", the output log prints out
QIODevice::read (QProcess): device not open
. -
Hi, I'm trying to get a live output out of a running thread, but I have a problem. When the command is supposed to finish, QProcess is still running. When I tried to run it without the live output, it worked fine.
Here's what I'm working with:QProcess fd; fd.setWorkingDirectory(dir + "platform-tools"); QStringList command; command << "fastboot devices"; fd.startDetached("powershell", command); while(fd.Running) { //without this while it works fine qDebug() << fd.readAll(); msleep(100); }
After the command is "finished", the output log prints out
QIODevice::read (QProcess): device not open
.I'm trying to get a live output out of a running thread,
Where is there a "thread" involved anywhere here?
Don't use
QProcess::startDetached()
if you want to wait for a subprocess to finish. Don't use it if you want to read output. You are also incorrectly using astatic
method when you think you are using an instance method.while(fd.Running)
Please show me in the documentation where
QProcess
has a member variable namedRunning
? It does not. There is anenum QProcess::ProcessState
which has a value of2
. So yourwhile
never exits.msleep(100);
Don't do this.
So basically you need to completely rewrite your code. There are plenty examples out there.
-
I'm trying to get a live output out of a running thread,
Where is there a "thread" involved anywhere here?
Don't use
QProcess::startDetached()
if you want to wait for a subprocess to finish. Don't use it if you want to read output. You are also incorrectly using astatic
method when you think you are using an instance method.while(fd.Running)
Please show me in the documentation where
QProcess
has a member variable namedRunning
? It does not. There is anenum QProcess::ProcessState
which has a value of2
. So yourwhile
never exits.msleep(100);
Don't do this.
So basically you need to completely rewrite your code. There are plenty examples out there.
Hi @JonB, I wrote it wrong. I meant get live output out of that QProcess.
I usedstartDeatched()
, because I thought I could get live output somehow.I changed it to this:
... above still the same ... fd.start("powershell", command); while(fd.state() == QProcess::Running) { qDebug() << fd.readAll(); } fd.waitForFinished(); //not sure where to put this
That also doesn't work. It just prints
""
. If I putwaitForFinished()
before the while loop, it doesn't even activate (because it starts after the process finishes).
Edit: If I putwaitForFinished()
inside the while loop, it kinda works, but the problem is, it doesn't output anything and runs only once. -
Hi @JonB, I wrote it wrong. I meant get live output out of that QProcess.
I usedstartDeatched()
, because I thought I could get live output somehow.I changed it to this:
... above still the same ... fd.start("powershell", command); while(fd.state() == QProcess::Running) { qDebug() << fd.readAll(); } fd.waitForFinished(); //not sure where to put this
That also doesn't work. It just prints
""
. If I putwaitForFinished()
before the while loop, it doesn't even activate (because it starts after the process finishes).
Edit: If I putwaitForFinished()
inside the while loop, it kinda works, but the problem is, it doesn't output anything and runs only once.@Sucharek said in QProcess doesn't stop when the command is finished:
while(fd.state() == QProcess::Running)
You should not do this either. No loops anywhere!
What is wrong with:
fd.start("powershell", command); fd.waitForFinished(); qDebug() << fs.readAllStrandardOutput(); qDebug() << fs.readAllStrandardError();
?
What does "I meant get live output out of that QProcess." mean? If you want output while it is running (instead of at the end) you must use signals & slots instead of
waitForFinished()
. Whether you will actually get output as it goes along depends on whether any output frompowershell
is buffered at its side, which you cannot do anything about. -
To expand what @JonB said, here is working code
void runPowerShellCommand() { // Console app, using puts(). QProcess command; command.start("powershell.exe", QStringList("ls")); if (!command.waitForStarted(2000)) { puts("Failed to start command"); exit(101); } if (!command.waitForFinished(5000)) { puts("Command hang"); command.terminate(); exit(102); } puts(command.readAllStandardOutput().constData()); }
-
@Sucharek
hi
you can try thisQString Adb::cmd(const QString &command) { QProcess P2; P2.start(command); P2.waitForFinished(-1); P2.setReadChannel(QProcess::StandardOutput); QTextStream reade2(&P2); QString line2,line,Out; while (reade2.readLineInto(&line2)) Out.append(line2 +'\n'); P2.setReadChannel(QProcess::StandardError); QTextStream reader(&P2); while (reader.readLineInto(&line)) Out.append(line +'\n'); P2.close(); return Out.trimmed(); }
then when you need output
qDebug()<< cmd("fastboot devices")
or in QString
QString output = cmd("fastboot devices")