Important: Please read the Qt Code of Conduct -

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

  • 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.write("dir C:\Users\John\n");
    // ping a website
    // It also can run an EXISTED python script:
    process.start("python C:\Users\John\");

    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




    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.

  • Lifetime Qt Champion


    I found this on the net to that allows to have it interactive.

    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 {
        QDetachableProcess(QObject *parent = 0)
            : QProcess(parent) {
        void detach() {
    int main(int argc, char *argv[]) {
        QDetachableProcess process;
        QString program = "cmd.exe";
        QStringList arguments = QStringList() << "/K" << "python.exe";;
                    [](QProcess::CreateProcessArguments *args) {
            args->flags |= CREATE_NEW_CONSOLE;
            args->startupInfo->dwFlags &=~ STARTF_USESTDHANDLES;
        process.start(program, arguments);
        return 0;

  • @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?


  • Lifetime Qt Champion

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


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


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

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

Log in to reply