1

I need some help with this: I want to start a script ("test.sh") from inside my code and read the output. The script is giving an output string in an interval of 3 seconds for 5 times. So the output string is generated 5 times with a pause of 3 seconds in between. I want to display the output strings in a QTextBrowser. The string should be added every 3 seconds (as it comes from the script). Therefore I connected the readyReadStandardOutput() signal from QProcess to a slot but this slot is never entered. The connect function returns true. So no problem there, I guess. There is some code:

void DialogWindow::startScript()
{
    QProcess* prcs = new QProcess(this);
    connect(prcs, SIGNAL(readyReadStandardOutput()), this, SLOT(slot_readyReadStandardOutput()));

    QString program = "./test.sh";
    prcs->setProgram(program);
    prcs->open();
    if(prcs->isOpen())
        prcs->execute("test.sh"); //this line is executed
    prcs->waitForStarted(6000);
    qDebug() << prcs->state();  // --> "QProcess::NotRunning"
    qDebug() << prcs->error(); // --> "QProcess::FailedToStart"
    qDebug() << prcs->errorString(); // --> "0x000000c1"
    qDebug() << prcs->workingDirectory(); // --> ""
}

void DialogWindow::slot_readyReadStandardOutput()
{
    QTextBrowser* m_pTextBrowser = new QTextBrowser();
    prcs->readAllStandardOutput();
    prcs->readAllStandardError();

    QString output = prcs->readAllStandardOutput();
    if(output.contains("warning"))
        m_pTextBrowser->setTextColor(QColor(Qt::yellow));
    if(output.contains("error"))
    {
        m_pTextBrowser->setTextColor(QColor(Qt::red));
        //emit errorOccured();    
}
    m_pTextBrowser->append(prcs->readAllStandardOutput());
    m_pTextBrowser->setTextColor(QColor(Qt::black));

}

What am I missing???? My script is located in my project folder and I copied it to the debug folder. The script is running properly and returns the output text as expected when double clicking it or using the command "./test.sh" in the shell.


UPDATE: I could figure out which error string I get (0x000000c1) and I looked it up here. It means: ERROR_BAD_EXE_FORMAT / %1 is not a valid Win32 application

What does that mean? The script is running as described above... Why can I not start it with QProcess?

Kali
  • 11
  • 2
  • 1
    What if you connect to https://doc.qt.io/qt-5/qprocess.html#readyReadStandardOutput signal? – pptaszni Jul 20 '22 at 13:33
  • Also you don't need to `QProcess* p = qobject_cast(sender());` because you already have `prcs` as your class field. – pptaszni Jul 20 '22 at 13:35
  • Is `test.sh` located in the directory from which you run the application? Also note that rather than use the `QProcess::waitFor*` member functions you're usually better off connecting to the various [signals](https://doc.qt.io/qt-6/qprocess.html#signals) to receive notifications. Likewise use the 'new' signal/slot syntax rather than the `SIGNAL(...)`/`SLOT(...)` macros. – G.M. Jul 20 '22 at 13:59
  • FYI: [Creating QT Application as GUI for existing console-based application on windows](https://stackoverflow.com/a/45209503/7478597) – Scheff's Cat Jul 20 '22 at 14:20
  • 1
    You have a huge amount of code just to demonstrate running a script. Can you trim your example a bit to be concise just to demonstrate the issue at hand? – László Papp Jul 20 '22 at 18:57
  • @pptaszni That ist what I'm doing. I am connecting readyRead and readyReadStandardOutput. Both is successful but the slot is never entered. But you're right, there's no object cast needed... – Kali Jul 27 '22 at 08:44
  • @G.M. The test.sh file is located in the project folder from Qt Creator (where my .pro is located) and additionally I copied it to the debug folder that is created by Qt Creator ("build-.....-Debug") and to the folder named "debug" within the debug folder from Qt Creator ("build-.....-Debug"). What do you mean by 'new signal/slot syntax'? – Kali Jul 27 '22 at 08:47
  • @LászlóPapp Of course. That is my first question on that platform and I wasn't sure about how much code is needed... Hope it's clearer now. – Kali Jul 27 '22 at 08:52
  • Mmm then dunno. On Windows that would not work, because of [explained here](https://stackoverflow.com/questions/70604074/starting-batch-file-using-qprocess-execute-on-different-windows-version), but if you are using normal system, then in general you should be able to execute something. Your example is still missing crucial parts (headers, main, call to `QApplication::exec`), and is superfluous at the same time. No need for `ProcessOutputHandler`, just create simple widget with a button, and when it's clicked run `QProcess::execute("./test.sh);`, and see if you got something. – pptaszni Jul 27 '22 at 14:21
  • @pptaszni why should this not work on windows? Because it is not a .exe file? – Kali Jul 29 '22 at 07:28
  • "Because it is not a .exe file" - apparently yes, it doesn't know how to interpret it unless you explicitly pass it an interpreter, like `cmd.exe script.bat`. This system is good for gaming and nothing else. Trying to develop any software on it is an endless pain. – pptaszni Jul 29 '22 at 07:37
  • @pptaszni thank you so much, I'm glad I could solve it now but as you say... working with windows really an endless pain.. – Kali Aug 01 '22 at 08:35

0 Answers0