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.
-
Any particular language?– Ignacio Vazquez-AbramsJan 27, 2010 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-AbramsJan 27, 2010 at 15:54
-
If you don't want to block your app while QProcess runs look at the answer to this question: stackoverflow.com/questions/10098980/…– parsley72Jan 25, 2014 at 6:15
Add a comment
|
3 Answers
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();
Use QProcess.
-
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. 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!– StJimmyJan 27, 2010 at 22:29