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.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
StJimmy
  • 504
  • 3
  • 9
  • 18

3 Answers3

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

Use QProcess.

e8johan
  • 2,899
  • 17
  • 20
  • 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. – 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

What about using popen?

t0mm13b
  • 34,087
  • 8
  • 78
  • 110