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. How to open console window via QProcess?
QtWS25 Last Chance

How to open console window via QProcess?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 5 Posters 4.9k Views
  • 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.
  • K Offline
    K Offline
    Kien Bui
    wrote on last edited by
    #1

    I init a QProcess to open a console window, but no window is show.

    QProcess *process = new QProcess();
    process->start("adb devices");
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      There's no reason for any console window to be shown when executing such a command.

      If you want to parse the output of your process then you can read what was sent to stdout once your command has finished.

      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
      3
      • K Offline
        K Offline
        Kien Bui
        wrote on last edited by Kien Bui
        #3

        To work with Android device, I need open a console window. I want to open a console window to continue text command
        Eg: "adb -s device shell" to open shell access android device

        1 Reply Last reply
        0
        • K Offline
          K Offline
          Kien Bui
          wrote on last edited by
          #4

          I found a keyword: "CREATE_NEW_CONSOLE" but I don't know to use it

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            Hi,
            What you want is called a interactive shell/prompt
            This is sample for Windows

            QDetachableProcess process;
                QString program = "cmd.exe";
                QStringList arguments = QStringList() << "/K" << "C:\\Program Files\\Inkscape\\python.exe";
                process.setCreateProcessArgumentsModifier(
                            [](QProcess::CreateProcessArguments *args) {
                    args->flags |= CREATE_NEW_CONSOLE;
                    args->startupInfo->dwFlags &=~ STARTF_USESTDHANDLES;
                });
                process.start(program, arguments);
                process.detach();
                }
            

            starts python and let u type commands in it.

            JonBJ 1 Reply Last reply
            2
            • mrjjM mrjj

              Hi,
              What you want is called a interactive shell/prompt
              This is sample for Windows

              QDetachableProcess process;
                  QString program = "cmd.exe";
                  QStringList arguments = QStringList() << "/K" << "C:\\Program Files\\Inkscape\\python.exe";
                  process.setCreateProcessArgumentsModifier(
                              [](QProcess::CreateProcessArguments *args) {
                      args->flags |= CREATE_NEW_CONSOLE;
                      args->startupInfo->dwFlags &=~ STARTF_USESTDHANDLES;
                  });
                  process.start(program, arguments);
                  process.detach();
                  }
              

              starts python and let u type commands in it.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @mrjj
              Yes, but the OP wants it for Android! :)

              mrjjM 1 Reply Last reply
              1
              • JonBJ JonB

                @mrjj
                Yes, but the OP wants it for Android! :)

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @JonB
                Oh, thx. i missed that little word.
                No idea how to get interactive shell on phone.
                if even allowed.

                1 Reply Last reply
                1
                • M Offline
                  M Offline
                  mnbv
                  wrote on last edited by mnbv
                  #8

                  Wanted to do something similar and found this while checking if QProcess could handle it conveniently (it can't, as seen above).

                  Just use std::system. Doing it with QProcess just makes extra work. E.g. for the OP:

                  #include <cstdlib>
                  
                  std::system("adb -s device shell");
                  

                  If you want to just open a command shell, the command depends on the platform but it'd be something like:

                  std::system("cmd"); // windows
                  std::system("/bin/bash"); // linux, maybe osx? 
                  // etc...
                  

                  For that Python example above:

                  std::system("\"C:\\Program Files\\Inkscape\\python.exe\"");
                  
                  // or if necessary:
                  std::system("cmd /K \"C:\\Program Files\\Inkscape\\python.exe\"");
                  
                  // or if python is in the path, just:
                  std::system("python");
                  

                  PS This whole thread is a bit silly. The OP didn't want to open a shell on a phone, and there's definitely good reasons to start adb in a console window (it's a console-based interactive shell for debugging an attached Android device). The OP wanted to start it in a console window so they could continue to use it in said console window after starting it.

                  1 Reply Last reply
                  0

                  • Login

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