Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QProcess write multiple command to the same session in same window (adb shell)
Forum Updated to NodeBB v4.3 + New Features

QProcess write multiple command to the same session in same window (adb shell)

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fadu
    wrote on 1 Jul 2022, 15:24 last edited by
    #1

    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
    Command Prompt - adb  shell 7_1_2022 6_18_03 PM.png
    this is the code i using now

    QString 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();
    }
    
    J 1 Reply Last reply 7 Jul 2022, 15:10
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 1 Jul 2022, 18:04 last edited by
      #2

      Hi,

      I haven't checked whether it's possible but you could start adb and then write the commands on the QProcess standard input.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • F Offline
        F Offline
        fadu
        wrote on 7 Jul 2022, 15:02 last edited by
        #3

        @SGaist
        sorry for my late response
        no it's each time start new window of adb and start from the first

        1 Reply Last reply
        0
        • F fadu
          1 Jul 2022, 15:24

          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
          Command Prompt - adb  shell 7_1_2022 6_18_03 PM.png
          this is the code i using now

          QString 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();
          }
          
          J Offline
          J Offline
          JonB
          wrote on 7 Jul 2022, 15:10 last edited by JonB 7 Jul 2022, 15:10
          #4

          @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.

          1 Reply Last reply
          1
          • F Offline
            F Offline
            fadu
            wrote on 8 Jul 2022, 09:07 last edited by
            #5

            @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 session

            J 1 Reply Last reply 8 Jul 2022, 12:32
            0
            • F fadu
              8 Jul 2022, 09:07

              @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 session

              J Offline
              J Offline
              JonB
              wrote on 8 Jul 2022, 12:32 last edited by JonB 7 Aug 2022, 12:34
              #6

              @fadu
              Then you must not make any waitForFinished() 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 your adb 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 the adb there, it will not exit the shell too). You will either need to pass an argument to the shell to tell it to run adb and then exit, or maybe you don't need any shell and can just run the adb directly.
              1 Reply Last reply
              2

              1/6

              1 Jul 2022, 15:24

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved