10

How can I run command-line programs under Linux from Qt4? And of course I want to obtain the output in some way I can use. I'd use it for an ls | grep, but it's good to know for any future issues.

3

3 Answers 3

24
QProcess p;
p.start( /* whatever your command is, see the doc for param types */ );
p.waitForFinished(-1);

QString p_stdout = p.readAllStandardOutput();
QString p_stderr = p.readAllStandardError();
2

Use QProcess.

2
  • 1
    For some tasks, it might be easier (or more robust) to roll your own code - you mention ls | grep, potentially QDir with a filter or a QRegExp might do what you need. For the general case, though, QProcess is absolutely the best way to go. Jan 27, 2010 at 15:43
  • That's a great observation. I think I'll try that another time but the question in this case was also somewhat oriented for the general case for future uses. Thanks anyway!
    – StJimmy
    Jan 27, 2010 at 22:29
0

What about using popen?

1
  • e8johan: True...I understand you want to use QT API but thought I'd give this answer. :)
    – t0mm13b
    Jan 27, 2010 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.