簡體   English   中英

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

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

我正在嘗試使用 QProcess 執行 python 控制台並在 QTextEdit 中顯示控制台的內容。 以下是我的主要代碼:

    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...");
    }


我使用QProcess::start("python")來啟動一個新進程和QProcess::write()來編寫我的 python 代碼,但是我在我的 QTextEdit 中看不到任何顯示,順便說一句,python 進程確實是根據到顯示“進程已啟動...”的 stateLabel。 如何讓python輸出顯示在QTextEdit中? 謝謝。

嘗試 :

        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)將合並輸出通道。 各種程序寫入不同的輸出。 有些使用錯誤輸出進行正常記錄,有些使用“標准”輸出,有些兩者兼而有之。 最好將它們合並。

readAll()讀取目前可用的所有內容。

它被放入一個帶有 waitForReadyRead(-1) 的循環中(-1 表示沒有超時),它會阻塞直到可以讀取某些內容。 這是為了確保實際讀取所有內容。 在進程完成后簡單地調用 readAll() 被證明是非常不可靠的(緩沖區可能已經是空的)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM