Cannot get the QProcess started
-
Could somebody kindly hep me to troubleshot this problem?
Here is my debug output.
I am basically wanting to put some valid Command and args to make sure
I have them coded / syntax correct.link text]( START int Form:: RunProcess_Asynch(QString Command, QStringList args )
"hcitool "
(" scan ")
error enum val = QProcess::FailedToStartHere is my full code, keep in mind it is under construction
// temorary connect here connect(&OProcess, &QProcess::errorOccurred, [=](QProcess::ProcessError error) { qDebug() << "error enum val = " << error << endl; }); #ifdef BYPASS connect(&OProcess, &QProcess::stateChanged, [=](QProcess::S::S::ProcessError error) { qDebug() << "error enum val = " << error << endl; }); #endif connect(&OProcess, &QProcess::stateChanged, this, &Form::processError); connect(&OProcess, &QProcess::errorOccurred, this, &Form::processError); //connect(&OProcess, &QProcess::finished(exit)::finished, this, &Form::processFinished); // connect(&OProcess, &QProcess::started, this, &Form::processError); connect(&OProcess, &QProcess::started, this, &Form::processStarted); // capture any errors (do this before you run the process) connect(&OProcess, &QProcess::errorOccurred, this, &Form::processError); Command = "ls "; args<<"-l"; //<<">> /tmp/temp"; OProcess.start(Command,args); //,QIODevice::ReadWrite); NOTE inlcuding ,QIODevice::ReadWrite); - it still fails to start OProcess.readAllStandardError(); // temorary connect here // connect(&OProcess, &QProcess::errorOccurred, this, &Form::processError); // connect(&OProcess, &QProcess::finished(exit)::finished, this, &Form::processFinished); // connect(&OProcess, &QProcess::started, this, &Form::processError); OProcess.waitForFinished(); qDebug(" END int Form:: RunProcess_Asynch(QString Command, QStringList args )"); return 0; }
-
@AnneRanch said in Cannot get the QProcess started:
Could somebody kindly hep me to troubleshot this problem?
This cannot work as it is, you have to choose which way you use
QProcess
:- asynchronous with signals/slots
- synchronous with QProcess::waitForXXXXX()
But you should not mix them up.
Another question, is
OProcess
a local variable or a class member? -
@KroMignon OProcess is a class member.
I need to get the process started , then I will modify the rest of the code.. -
Partially solved.
The QPrtocess will not start if there are any spaces in the "command' or "arg".
Still now writing to the file or console. .Addendum
I need clarification on this
I did venture to QProcess to have a better control then afforded by "system" calls.
After all this I realized that I may have a better understanding of QProcess, however, I have a major misunderstanding of trying to replace
system("hcotool dev >> /tmp/temp")with
//Command = "hcitool";
// args<<"dev"<<">>/tmp/temp";
Oprocess.start(Command,args,QIODevice::ReadWrite);Basically
how does “command , Arg “ get passed to the system similar to “system” call ?
On top of that – I really do not understand what “device” is QIODevice
accessing. My gut feeling is that this “OProcess.start” is incomplete to fully replace “system” call. -
@AnneRanch said in Cannot get the QProcess started:
I need clarification on this
I did venture to QProcess to have a better control then afforded by "system" calls.
After all this I realized that I may have a better understanding of QProcess, however, I have a major misunderstanding of trying to replace
system("hcotool dev >> /tmp/temp")I don't really understand why you want to add file redirection in command? The output of your command will be avaible with QProcess::readAllStandardOutput().
I guess the command returns immediatly after execution, so I would do it like this:
QProcess hcitool; hcitool.setProcessChannelMode(QProcess::MergedChannels); hcitool.start("hcitool", QStringList() << "dev"); if(hcitool.waitForFinished(1000)) { QString text(hcitool.readAll()); qDebug() << "hcitool returns:" << text; } else { qDebug() << "Failed to run hcitool"; }
-
@AnneRanch said in Cannot get the QProcess started:
After all this I realized that I may have a better understanding of QProcess, however, I have a major misunderstanding of trying to replace
system("hcotool dev >> /tmp/temp")I may not get much thanks for this, but to answer just this question. You cannot pass
>>
or>> /tmp/temp
as an argument toQProcess
. This is because only the Linux shell deals with redirection symbols such as>>
. (This would also be the case if you use other redirection symbols such as>
,<
or|
.)When you go
system("hcitool dev >> /tmp/temp")
what the library functionsystem()
does is issue this command:/bin/sh -c "hcitool dev >> /tmp/temp"
, which gets the/bin/sh
shell to execute the command dealing with the>>
redirection.To do the same from
QProcess
you need to go:OProcess.start("/bin/sh", QStringList() << "-c" << "hcitool dev >> /tmp/temp"); // or shorter if you prefer, same thing: OProcess.start("/bin/sh", { "-c", "hcitool dev >> /tmp/temp" });
That will work.
A separate issue is that you would be better not doing it this way and instead handling the output redirection to a file or memory yourself, as @KroMignon shows one way. But if you want to do it the same way as your original
system()
call this is how to do it viaQProcess
. -
-
@Christian-Ehrlicher LOL.
@AnneRanch In that link you asked that question before and I answered the same way. That remains valid.
-
OK, let me try this again.
Here a full codevoid Form::on_pushButton_46_clicked() { qDebug(" START void Form::on_pushButton_46_clicked()"); ReadTempFile(); ClearTempFile(); system("hciconfig -a >/tmp/temp"); // TOK builds file ReadTempFile(); ClearTempFile(); QProcess process; qDebug(" START process.start..... -a "); process.start("hciconfig", QStringList() << "-a"); // no output expected process.waitForFinished(); qDebug(" process.waitForFinished() hcicofig -a "); ReadTempFile(); // no data in tempo expected process.start("hciconfig", QStringList() << "-a"<<" >>"<<"/tmp/temp"); **THIS FAILS TO REDIRECT THE HCICONFIG TO TEMP FILE** process.waitForFinished(); qDebug(" process.waitForFinished() hcicofig -a >>/tmp/temp"); qDebug(" process.waitForFinished()"); ReadTempFile(); qDebug(" NO DATA ReadTempFile()"); process.start("hcitool", QStringList() << "dev"<<">>"<<"/tmp/temp"); process.waitForFinished(); process.start("hcitool", QStringList() << "dev"<<">>/tmp/temp"); process.waitForFinished(); qDebug(" End void Form::on_pushButton_46_clicked()"); } Here is the output.
10:46:25: Starting /home/qy/Qt/Examples/Qt-5.12.12/widgets/mainwindows/mdi/mdi...
Warning: Ignoring XDG_SESSION_TYPE=wayland on Gnome. Use QT_QPA_PLATFORM=wayland to run on Wayland anyway.
START void Form::on_pushButton_46_clicked()
START Form::ReadTempFile()
in.readLine().isEmpty()
END Form::ReadTempFile()
START void Form::ClearTempFile()
in.readLine().isEmpty()
END void Form::ClearTempFile()
START Form::ReadTempFile()
END Form::ReadTempFile()
START void Form::ClearTempFile()
in.readLine().isEmpty()
END void Form::ClearTempFile()
START process.start..... -a
process.waitForFinished() hcicofig -a
START Form::ReadTempFile()
in.readLine().isEmpty()
END Form::ReadTempFile()
process.waitForFinished() hcicofig -a >>/tmp/temp
process.waitForFinished()
START Form::ReadTempFile()THetre ARE no DATA IN TEMP FILE
in.readLine().isEmpty()
END Form::ReadTempFile()
NO DATA ReadTempFile()
End void Form::on_pushButton_46_clicked()**The task of QProcess is to redirect output of command hciconfig to the temporary file.
This line of code fails to do so:
process.start("hciconfig", QStringList() << "-a"<<" >>"<<"/tmp/temp");**
Can anybody help me to solve this?
Do I have wrong syntax? -
@AnneRanch said in Cannot get the QProcess started:
Can anybody help me to solve this?
Do I have wrong syntax?Do you read what other replies to your post?
For example wat @JonB has writtento do the same from QProcess you need to go:
OProcess.start("/bin/sh", QStringList() << "-c" << "hcitool dev >> /tmp/temp");
// or shorter if you prefer, same thing:
OProcess.start("/bin/sh", { "-c", "hcitool dev >> /tmp/temp" });So this would be:
process.start(/bin/sh", QStringList() << "-c" << "hciconfig -a >> /tmp/temp");
-
@KroMignon said in Cannot get the QProcess started:
Do you read what other replies to your post?
I would guess no - the answer was now written three times, the first one already one and a half year ago for the exact same question. Really a nice and much to long active troll.
-
@KroMignon OK, I will sincerely apologize for all the trouble I have caused.
I have made few major mistakes-
QProcess is useless without monitoring - hence it is important to utilize as many "connect" as feasible to keep track what QProcess is doing..
-
In any case process "start" has to be immediately followed - in code - waitForFinished(); Without that the process gets killed and without appropriate "connect" etc etc
-
I did not read the explanation what Linux and "system" does with "redirecting " AND THAT IS THE MOST IMPORTANT POST in this discussion. I was not aware about the details of "system" call. I look very briefly at the post and did see no point in replacing a simple "system" call , with another application. Many thanks for posting that.
... and I think it is a time to close this thread....
Solved? YES -
-
@AnneRanch said in Cannot get the QProcess started:
... and I think it is a time to close this thread....
Solved? YESNice to see you could solve your issue, but I am not really sure that you have understand what you have done wrong.
Qt is an asynchronous framework, but some classes can be used in synchronous mode.
QProcess
is one of them, but you should avoid mixing synchronous and asynchronous calls.That was, for me, the main issue in your code.
What I don't understand is why you want to use
QProcess
to mimicsystem()
calls.
For me, usingQProcess
made only sense when you want to take control about process execution like:- get process exit code
- get process output
If you only want to start a process and redirect its output to a file, using
QProcess
is a little bit an "overkill". -
@AnneRanch said in Cannot get the QProcess started:
In any case process "start" has to be immediately followed - in code - waitForFinished(); Without that the process gets killed and without appropriate "connect" etc etc
This is not true. You need to understand the scope of the
QProcess
variable in your C++ code and declare it in the appropriate place for your desired lifetime. -
@JonB
FYI
Contrary to documentation , perhaps it varies with QT version , the attached code (KISS) fills the /temp file - which was my objective .process.startDetached("hcitool dev>>/tmp/temp"); process.waitForFinished();
-
@AnneRanch said in Cannot get the QProcess started:
FYI
Contrary to documentation , perhaps it varies with QT version , the attached code (KISS) fills the /temp file - which was my objective .process.startDetached("hcitool dev>>/tmp/temp"); process.waitForFinished();
Why do you want to use
startDetached()
?
This doesn't make sense to me.
startDetached()
is for starting a daemon process, even the return PID held by theQProcess
instance may become invalid without notice (as written in documentation).
https://doc.qt.io/qt-5/qprocess.html#startDetached -
@AnneRanch said in Cannot get the QProcess started:
process.startDetached("hcitool dev>>/tmp/temp");
Apart from @KroMignon's correct point that you should not change to
startDetached()
.>> I believe you will find that your statement is not working, and you are mistaken in thinking it does. <<
I tested
process.startDetached("ls -l >>/tmp/temp");
under Ubuntu 20.04, with Qt 5.12.x. After executing your line, if you look in QT Creator's Application Output pane you get:/usr/bin/ls: cannot access '>>/tmp/temp': No such file or directory
Then I tried
process.startDetached("hcitool dev>>/tmp/temp");
and the Application Output showed justDevices:
In neither case is
/tmp/temp
created or written to.This is exactly as I would expect and have previously said above: you cannot use a redirection symbol like
>>
without going via a shell (/bin/sh
or/bin/bash
followed by-c
and then the command). Which is what this whole thread is about. Since you don't, the command (ls
orhcitool
) sees a single argument of>>/tmp/temp
, and cannot act on it correctly.I would guess you had the file
/tmp/temp
already created from a previous run, filled with previoushcitool
output, and thought that meant your command had been successful and appended to it, but it had not. Start byrm /tmp/temp
to ENSURE that file does not exist, and then try yourstartDetached()
command again. It does NOT create that file, does it...?