QProcess write multiple command to the same session in same window (adb shell)
-
wrote on 1 Jul 2022, 15:24 last edited by
hi
i build an app to send some command from pc to android phone
it's working great when i send single command but in some proccess i need to write multiple command in the same session
this is the code i using nowQString 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(); }
-
Hi,
I haven't checked whether it's possible but you could start adb and then write the commands on the QProcess standard input.
-
hi
i build an app to send some command from pc to android phone
it's working great when i send single command but in some proccess i need to write multiple command in the same session
this is the code i using nowQString 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(); }
wrote on 7 Jul 2022, 15:10 last edited by JonB 7 Jul 2022, 15:10@fadu
QProcess really isn't doing anything other than allowing you access process's stdin/out/err. Find out what exactly you need to send/receive from outside your program/Qt, e.g. from a Command Prompt/with file redirections.Also, if you mean you want to talk to the stdin of an ongoing process you will never want
P2.waitForFinished()
. If you need to have a "conversation" with the process you will need to use signals & slots, and reads & writes. -
@JonB
i want to write to standard input in same process (like in the pic above)
can you explain a small example for working with signal in this case
bcz adb exe didn't emit finish when you are in shell sessionwrote on 8 Jul 2022, 12:32 last edited by JonB 7 Aug 2022, 12:34@fadu
Then you must not make anywaitForFinished()
call.start()
to launch the process.QProcess::write()
to send to the subprocess.- Attach slot to
readyRead()
signal, this will be called any time the process receives bytes on its stdin from subprocess sending to either stdout or stderr. readAllStandardOutput
/Error()
from the slot to read whatever subprocess sends back and is available on these. Or you may choose to merge the channels, so only one to read from, see the docs.- Attach slot to
finished()
signal for when subprocess exits. It does emit this signal. You cannot expect it to know youradb
has finished because you are running it in an interactive terminal/shell as you show. You cannot afford to do this if you need to know when it terminates, because the shell keeps running (as you can see now by exiting theadb
there, it will not exit the shell too). You will either need to pass an argument to the shell to tell it to runadb
and then exit, or maybe you don't need any shell and can just run theadb
directly.
1/6