2015-12-16 119 views
-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(); 
} 

的输出随着make命令的输出是:POPEN不捕获所有的命令

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

随着ping命令的输出是:

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 

正如您所看到的,通过make命令,输出被捕获并显示qDebug但是,ping并非如此。

无论它是一个错误还是什么,我希望每个输出都可以通过我的程序通过qDebug被捕获和显示。

我现在应该做什么?

回答

5

在您的代码:

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

,你可以使用任何外壳命令是在机器上有用。

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

延伸阅读:

虽然在技术上是可以打开多个管道的子进程,它比单行调用popen要复杂得多: