简体   繁体   English

如何在win10上使用Qt执行python控制台?

[英]How to execute a python console using Qt on win10?

I'm trying to execute a python console using QProcess and show the content of the console in a QTextEdit.我正在尝试使用 QProcess 执行 python 控制台并在 QTextEdit 中显示控制台的内容。 Below is my major codes:以下是我的主要代码:

    connect(&process, SIGNAL(readyReadStandardOutput()), this, SLOT(PrintStandardOutput()));
    connect(&process, SIGNAL(started()), this, SLOT(Started()));
    ...
    void Widget::PrintStandardOutput()
    {
        QString stdOtuput = QString::fromLocal8Bit(process.readAllStandardOutput());
        ui->textEdit_Output->append(stdOtuput);
    }

    void Widget::Started()
    {
        ui->stateLabel->setText("Process started...");
    }


I used QProcess::start("python") to start a new process and QProcess::write() to write my python codes, but I can see nothing shown in my QTextEdit, by the way, the python process is indeed executed according to the stateLabel showning "Process started...".我使用QProcess::start("python")来启动一个新进程和QProcess::write()来编写我的 python 代码,但是我在我的 QTextEdit 中看不到任何显示,顺便说一句,python 进程确实是根据到显示“进程已启动...”的 stateLabel。 How to let the python output shown in the QTextEdit?如何让python输出显示在QTextEdit中? Thanks.谢谢。

Try :尝试 :

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

        connect(&process, SIGNAL(started()), this, SLOT(Started()));

        process.setProcessChannelMode(QProcess::MergedChannels);
        process.start(processToStart, arguments)

        void Widget::PrintStandardOutput()
        {

            // Get the output
            QString output;
            if (process.waitForStarted(-1)) {
                while(process.waitForReadyRead(-1)) {
                    output += process.readAll();
                    ui->textEdit_Output->append(output);
                }
            }
            process.waitForFinished();
        }

        void Widget::Started()
        {
        ui->stateLabel->setText("Process started...");
        }

setProcessChannelMode(QProcess::MergedChannels) will merge the output channels. setProcessChannelMode(QProcess::MergedChannels)将合并输出通道。 Various programs write to different outputs.各种程序写入不同的输出。 Some use the error output for their normal logging, some use the "standard" output, some both.有些使用错误输出进行正常记录,有些使用“标准”输出,有些两者兼而有之。 Better to merge them.最好将它们合并。

readAll() reads everything available so far. readAll()读取目前可用的所有内容。

It is put in a loop with waitForReadyRead(-1) (-1 means no timeout), which will block until something is available to read.它被放入一个带有 waitForReadyRead(-1) 的循环中(-1 表示没有超时),它会阻塞直到可以读取某些内容。 This is to make sure that everything is actually read.这是为了确保实际读取所有内容。 Simply calling readAll() after the process is finished has turned out to be highly unreliable (buffer might already be empty).在进程完成后简单地调用 readAll() 被证明是非常不可靠的(缓冲区可能已经是空的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM