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 execute a jit python console using QProcess on win10 ?

How to execute a jit python console using QProcess on win10 ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 971 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.
  • M Offline
    M Offline
    Mactarvish
    wrote on 17 Feb 2020, 16:27 last edited by
    #1

    I'm trying to develop a custom GUI command console using QProcess, the main connection code is like this:

        connect(&process, SIGNAL(readyReadStandardOutput()), this, SLOT(PrintStandardOutput()));
        connect(&process, SIGNAL(readyReadStandardError()), this, SLOT(PrintStandardError()));
    

    I used QProcess::start() to start a new process and QProcess::write() to write my subsequent instructions. Now this program can execute most commands like cmd and dir correctly in this way:

    // dir some path
    process.start("cmd");
    process.waitForStarted();
    process.write("dir C:\Users\John\n");
    // ping a website
    process.kill();
    process.start("ping www.google.com");
    // It also can run an EXISTED python script:
    process.start("python C:\Users\John\test.py");
    

    And I can see the execution result in my widget due to the connections above.

    But when I tried to execute a python console (not running an existed python script) using

    process.start("C:\Users\John\Anaconda3\python.exe");
    

    or

    process.start("cmd");
    process.waitForStarted();
    process.write("C:\Users\John\Anaconda3\python.exe");
    

    I can neither see the python interface (like the input symbol ">>>" ) shown in my GUI. What's the different between python command console and win10 cmd? How can I execute a just in time python console using QProcess or Qt? Thanks in advance.

    M J 2 Replies Last reply 17 Feb 2020, 17:00
    0
    • M Mactarvish
      17 Feb 2020, 16:27

      I'm trying to develop a custom GUI command console using QProcess, the main connection code is like this:

          connect(&process, SIGNAL(readyReadStandardOutput()), this, SLOT(PrintStandardOutput()));
          connect(&process, SIGNAL(readyReadStandardError()), this, SLOT(PrintStandardError()));
      

      I used QProcess::start() to start a new process and QProcess::write() to write my subsequent instructions. Now this program can execute most commands like cmd and dir correctly in this way:

      // dir some path
      process.start("cmd");
      process.waitForStarted();
      process.write("dir C:\Users\John\n");
      // ping a website
      process.kill();
      process.start("ping www.google.com");
      // It also can run an EXISTED python script:
      process.start("python C:\Users\John\test.py");
      

      And I can see the execution result in my widget due to the connections above.

      But when I tried to execute a python console (not running an existed python script) using

      process.start("C:\Users\John\Anaconda3\python.exe");
      

      or

      process.start("cmd");
      process.waitForStarted();
      process.write("C:\Users\John\Anaconda3\python.exe");
      

      I can neither see the python interface (like the input symbol ">>>" ) shown in my GUI. What's the different between python command console and win10 cmd? How can I execute a just in time python console using QProcess or Qt? Thanks in advance.

      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 17 Feb 2020, 17:00 last edited by mrjj
      #2

      @Mactarvish

      Hi
      I found this on the net to that allows to have it interactive.
      https://stackoverflow.com/questions/48518442/qt-start-process-in-interactive-shell/48518689

      Not 100% sure its what you are looking for but it will open a working python shell.

      
      #include <QProcess>
      #include <QString>
      #include <QStringList>
      #include "Windows.h"
      
      class QDetachableProcess
              : public QProcess {
      public:
          QDetachableProcess(QObject *parent = 0)
              : QProcess(parent) {
          }
          void detach() {
             waitForStarted();
             setProcessState(QProcess::NotRunning);
          }
      };
      
      int main(int argc, char *argv[]) {
          QDetachableProcess process;
          QString program = "cmd.exe";
      
          QStringList arguments = QStringList() << "/K" << "python.exe";;
          process.setCreateProcessArgumentsModifier(
                      [](QProcess::CreateProcessArguments *args) {
              args->flags |= CREATE_NEW_CONSOLE;
              args->startupInfo->dwFlags &=~ STARTF_USESTDHANDLES;
          });
          process.start(program, arguments);
          process.detach();
          return 0;
      }
      
      
      M 1 Reply Last reply 18 Feb 2020, 05:18
      1
      • M mrjj
        17 Feb 2020, 17:00

        @Mactarvish

        Hi
        I found this on the net to that allows to have it interactive.
        https://stackoverflow.com/questions/48518442/qt-start-process-in-interactive-shell/48518689

        Not 100% sure its what you are looking for but it will open a working python shell.

        
        #include <QProcess>
        #include <QString>
        #include <QStringList>
        #include "Windows.h"
        
        class QDetachableProcess
                : public QProcess {
        public:
            QDetachableProcess(QObject *parent = 0)
                : QProcess(parent) {
            }
            void detach() {
               waitForStarted();
               setProcessState(QProcess::NotRunning);
            }
        };
        
        int main(int argc, char *argv[]) {
            QDetachableProcess process;
            QString program = "cmd.exe";
        
            QStringList arguments = QStringList() << "/K" << "python.exe";;
            process.setCreateProcessArgumentsModifier(
                        [](QProcess::CreateProcessArguments *args) {
                args->flags |= CREATE_NEW_CONSOLE;
                args->startupInfo->dwFlags &=~ STARTF_USESTDHANDLES;
            });
            process.start(program, arguments);
            process.detach();
            return 0;
        }
        
        
        M Offline
        M Offline
        Mactarvish
        wrote on 18 Feb 2020, 05:18 last edited by
        #3

        @mrjj
        Thanks for your reply. But actually I want the standard output of python console shown in my GUI instead of recreating a new window. I'm wondering that both cmd and python are interactive consoles, why the cmd interactive interface can be shown in my GUI, but not python?

        5800d2fe-4450-4d6b-b435-f5a48fc181af-image.png
        153b7ccc-fab6-404b-8817-fc6edda992f3-image.png

        1 Reply Last reply
        0
        • M Mactarvish
          17 Feb 2020, 16:27

          I'm trying to develop a custom GUI command console using QProcess, the main connection code is like this:

              connect(&process, SIGNAL(readyReadStandardOutput()), this, SLOT(PrintStandardOutput()));
              connect(&process, SIGNAL(readyReadStandardError()), this, SLOT(PrintStandardError()));
          

          I used QProcess::start() to start a new process and QProcess::write() to write my subsequent instructions. Now this program can execute most commands like cmd and dir correctly in this way:

          // dir some path
          process.start("cmd");
          process.waitForStarted();
          process.write("dir C:\Users\John\n");
          // ping a website
          process.kill();
          process.start("ping www.google.com");
          // It also can run an EXISTED python script:
          process.start("python C:\Users\John\test.py");
          

          And I can see the execution result in my widget due to the connections above.

          But when I tried to execute a python console (not running an existed python script) using

          process.start("C:\Users\John\Anaconda3\python.exe");
          

          or

          process.start("cmd");
          process.waitForStarted();
          process.write("C:\Users\John\Anaconda3\python.exe");
          

          I can neither see the python interface (like the input symbol ">>>" ) shown in my GUI. What's the different between python command console and win10 cmd? How can I execute a just in time python console using QProcess or Qt? Thanks in advance.

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 18 Feb 2020, 06:58 last edited by
          #4

          @Mactarvish said in How to execute a jit python console using QProcess on win10 ?:

          process.start("C:\Users\John\Anaconda3\python.exe");

          Do you really do it like this? You need to escape back-slashes:

          process.start("C:\\Users\\John\\Anaconda3\\python.exe");
          

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          M 1 Reply Last reply 18 Feb 2020, 07:24
          0
          • J jsulm
            18 Feb 2020, 06:58

            @Mactarvish said in How to execute a jit python console using QProcess on win10 ?:

            process.start("C:\Users\John\Anaconda3\python.exe");

            Do you really do it like this? You need to escape back-slashes:

            process.start("C:\\Users\\John\\Anaconda3\\python.exe");
            
            M Offline
            M Offline
            Mactarvish
            wrote on 18 Feb 2020, 07:24 last edited by
            #5

            @jsulm Hi, as you can see in the image above, I actually used a QLineEdit to type my command, and
            "C:\\Users\\John\\Anaconda3\\python.exe"
            or
            "C:\Users\John\Anaconda3\python.exe"
            or just
            "python"
            can find the correct path of python.exe, but the problem still exists.

            J 1 Reply Last reply 18 Feb 2020, 09:53
            0
            • M Mactarvish
              18 Feb 2020, 07:24

              @jsulm Hi, as you can see in the image above, I actually used a QLineEdit to type my command, and
              "C:\\Users\\John\\Anaconda3\\python.exe"
              or
              "C:\Users\John\Anaconda3\python.exe"
              or just
              "python"
              can find the correct path of python.exe, but the problem still exists.

              J Offline
              J Offline
              JonB
              wrote on 18 Feb 2020, 09:53 last edited by JonB
              #6

              @Mactarvish
              There are many possibilities as to why this may or may not be possible, depending on how python.exe behaves with input/output redirection.

              I would start with: set your streams to be unbuffered, because there is a chance that output from python is still at the other end, not reaching your application. Easiest is to launch as python -u. (Do not use CMD.)

              Secondly/independently, ignore output for now. Verify whether your host can use a write() to send it a python command which it acts upon successfully (e.g. create a file, or something you can test like that).

              1 Reply Last reply
              0

              6/6

              18 Feb 2020, 09:53

              • Login

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