![](/img/trans.png)
[英]How would I get input from a QLineEdit in a Qt Creator GUI?
[英]How to run Python script from QT creator and print output to GUI
void MainWindow::on_pushButton_clicked()
{
QProcess p;
// get values from ini file
settings->setValue("EMail", ui->lineEditEMail->text());
settings->setValue("Password", ui->lineEditPassword->text());
settings->setValue("Chronological", ui->checkBox->isChecked());
settings->setValue("Current_info", ui->checkBox_2->isChecked());
settings->endGroup();
settings->sync();
// launch python code for login
QString program( "C:/projects/build-test3-Desktop_Qt_6_4_0_MinGW_64_bit-Debug/venv/Scripts/python.exe");
QStringList args = QStringList() << "index.py";
QProcess::execute( program, args );
}
我有這個 function 在單擊按鈕后執行,我需要將“index.py”的 output 打印到我的應用程序中。 我應該使用什么小部件以及如何使用? 從我讀到的 QTextBrowser 應該可以解決問題,但我不確定如何使用它。
這就是我的 GUI 的樣子。 我想在按鈕右側某處使用 output 我的結果。 我還沒有添加小部件,因為我不確定 QTextBrowser 是否是我需要的
您可以用於此目的的小部件是QTextEdit
(您可以從圖形用戶界面將其設置為只讀)。
但是如果你想得到執行的 output,你需要一個合適的QProcess
實例並調用QProcess::readAllStandardOutput()
成員 function 來得到標准的 Z78E6221F6393D135CEDZ868F。
您可能還對QProcess::readAllStandardError()
感興趣,以便在失敗時獲取錯誤。
編輯(簡單/基本示例):
QProcess p;
p.start("path/to/python.exe", QStringList("script.py"));
p.waitForFinished();
QByteArray p_stdout = p.readAllStandardOutput();
QByteArray p_stderr = p.readAllStandardError();
// Do whatever you want with the results (check if they are not empty, print them, fill your QTextEdit contents, etc...)
注意:如果您不想被QProcess::waitForFinished()
阻塞,您可以在QProcess::finished()
信號上使用信號/插槽連接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.