2

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; }
1
  • 1
    I would use QProcess instead of this. Commented May 2, 2017 at 22:35

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

what 's the QString program parameter should be ?
I did it, thanls again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.