How to open console window via QProcess?



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

    QProcess *process = new QProcess();
    process->start("adb devices");
    

  • Lifetime Qt Champion

    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.



  • 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



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


  • Lifetime Qt Champion

    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.



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


  • Lifetime Qt Champion

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



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


Log in to reply