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.
Asked
Active
Viewed 1.5k times
10

Gilles 'SO- stop being evil'
- 104,111
- 38
- 209
- 254

StJimmy
- 504
- 3
- 9
- 18
-
Any particular language? – Ignacio Vazquez-Abrams Jan 27 '10 at 15:39
-
6Except when you're talking about one of its bindings (yes, I've seen that happen), but I'll let it slide. – Ignacio Vazquez-Abrams Jan 27 '10 at 15:54
-
If you don't want to block your app while QProcess runs look at the answer to this question: http://stackoverflow.com/questions/10098980/real-time-display-of-qprocess-output-in-a-textbrowser – parsley72 Jan 25 '14 at 06:15
3 Answers
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();

Fred
- 4,894
- 1
- 31
- 48
2
-
1For 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. – James Turner Jan 27 '10 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 '10 at 22:29
0
-
e8johan: True...I understand you want to use QT API but thought I'd give this answer. :) – t0mm13b Jan 27 '10 at 15:55