-1
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QProcess>
#include <QFile>
#include <QDebug>
#include <stdio.h>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    FILE* file1 = popen ("make", "r");

    char buff[5122];

    while(fgets(buff, sizeof(buff), file1)!=NULL)
    {
        qDebug() << "from here: " << buff;
    }


    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral ("qrc:/main.qml")));
    return app.exec();
}

With make command the output is:

QML debugging is enabled. Only use this in a safe environment.
from here: make: Nothing to be done forfirst'.`

With ping command the output is:

QML debugging is enabled. Only use this in a safe environment.
Usage: ping [-aAbBdDfhLnOqrRUvV] [-c count] [-i interval] [-I interface]
            [-m mark] [-M pmtudisc_option] [-l preload] [-p pattern] [-Q tos]
            [-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp_option]
            [-w deadline] [-W timeout] [hop1 ...] destination

As you can see, with make command the output was trapped and displayed by qDebug. But, this isn't the case with ping.

Be it an error or whatever, I want every output to be trapped and displayed through qDebug through my program.

What am I supposed to do now?

Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411

1 Answers1

5

In your code:

FILE* file1 = popen ("make", "r");

you can use whatever shell command is useful on the machine. For instance, you can redirect the standard error to the same destination as the standard output, and capture both via the pipe:

FILE* file1 = popen ("make 2>&1", "r");

Further reading:

While it is technically possible to open multiple pipes to a subprocess, it is much more complicated than the single-line call to popen:

Community
  • 1
  • 1
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105