0
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 );

}

I have this function that is executed after a button is clicked and I need to print the output of "index.py" in to my app. What widget should I use and how? From what I read QTextBrowser should do the trick but I'm not sure how to use it. GUI

This is how my GUI looks like. I'd like to use to output my results somewhere in button right. I didn't add the widget yet, because I'm not sure QTextBrowser is the one I need

Modestas
  • 55
  • 5

1 Answers1

0

The widget you could use for this purpose is QTextEdit (you can set it to be read-only from the graphical user interface).

But if you want to get the output of the execution, you will need a proper instance of QProcess and call the QProcess::readAllStandardOutput() member function to get the standard output.

You may also be interested by QProcess::readAllStandardError() to get the errors in case of failure.


Edit (simple/basic example):

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

Note: If you don't want to be blocking with QProcess::waitForFinished(), you can use a signal/slots connection on QProcess::finished() signal.

Fareanor
  • 5,900
  • 2
  • 11
  • 37
  • What is the proper instance of QProcess? Why is mine bad? – Modestas Aug 17 '22 at 11:25
  • Yours is not bad, you have none. You are calling a `static` function of `QProcess` class. But for more advanced usage (like getting the output for example), you will need an actual instance of `QProcess`, not only `static` calls. An instance on which you could use non-static member functions like `QProcess::readAllStandardOutput()` for example :) – Fareanor Aug 17 '22 at 11:26
  • Do you have an example by any chance? – Modestas Aug 17 '22 at 11:28
  • 1
    Ouch, if you don't know how to create an instance of a class, I would advise you to start by getting a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and take the time to learn C++ properly because it really is the basics of the basics. By the way, in your function, you already have an (unused) instance of `QProcess` that you called `p`. – Fareanor Aug 17 '22 at 11:32
  • @Modestas I have given a small example about how to get the output with `QProcess`, I leave the rest to you. StackOverflow is not a free coding service :) – Fareanor Aug 17 '22 at 11:46
  • Yeah, I know. I'll try to pull it off. Thank you for your comments and assistance – Modestas Aug 17 '22 at 11:49