Sameh Mohamed Omr
Sameh Mohamed Omr

Reputation: 37

Get qt application output to Qwidget c++

I build an app that execute linux command through C++ Qt GUI I read from the file and show the output normally but sometimes the output from file is data = "" and the output that - in normal show in terminal - show in Application output so I want to get a application output to Qwidget such as QTextEdit

like
cat:: /home/user/Desktop: Is ad directory , the function I used it is

QString operation :: commands(std::string usercommand){
const char * convertor = userCommand.c_str();
string data;
FILE *f =popen(convertor,"r");
char buffer [1024];
while (fgets(buffer,sizeof(buffer)-1,f)!=NULL){data=data+buffer;}
pclose(f);
QString returning = QString::fromStdString(data);  return returning; }

Upvotes: 2

Views: 267

Answers (1)

eyllanesc
eyllanesc

Reputation: 243917

If you are working with Qt you should use QProcess

QString operation::commands(QString program)
{    
    QProcess process;
    process.start(program);
    while (process.waitForFinished()){
        ;
    }
    QString resp = QString::fromLocal8Bit(process.readAllStandardOutput());
    QString error = QString::fromLocal8Bit(process.readAllStandardError());
    return resp + error;
}

Use:

QString usercommand = "cat /home/user/Desktop";
commands(usercommand);

Upvotes: 1

Related Questions