Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get output system() command in Qt?

I use system() command in Qt. and I want to get output and show it to users. my command is:

system("echo '" + rootPass.toAscii() + "' | su - root -c 'yum -y install " + packageName.toAscii() + "'");

this command can't run when I use it in QProcess (start or execute function) but if i can run this command in QProcess i can get output with QProcess::readAllStandardOutput() function.

also when i used ">" in system command to save output in a file, I receive output when the package completely installed. like bellow:

system("echo '" + rootPass.toAscii() + "' | su - root -c 'yum -y install " + packageName.toAscii() + "' > result.out");

is there any idea about running this command with QProcess, or get output from system() command as soon as write each line.

like image 890
mohsen amiri Avatar asked Oct 16 '13 17:10

mohsen amiri


People also ask

How to get output of system command in Qt?

system("echo '" + rootPass. toAscii() + "' | su - root -c 'yum -y install " + packageName. toAscii() + "' > result. out");

How do you use command line arguments in Qt?

There is a Arguments line edit where you can put all you need to pass to your app when launching it. For Qt Creator from Qt 5.6 Go in the "Projects part on the left and then in the "Build & Run" tab. Here you have a "Command line arguments" edit where you can put all parameters you want to pass to your app.

How do I run a Linux command in Qt?

@grullo said in Run command line from Qt app in linux: QString cmdline = ". /home/grullo/xmip-bundle/build/xmipp. bashrc"; I guess the initial dot (".") is making your command line relative, so that path is not found when set the working directory.


2 Answers

You can also obtain the output directly from QProcess

QProcess process;
process.start(/* command line stuff */);
process.waitForFinished(-1); // will wait forever until finished

QString stdout = process.readAllStandardOutput();
QString stderr = process.readAllStandardError();

If you don't want to block your event loop, you can always use the signals:

readyReadStandardOutput();
readyReadStandardError();

And then call a function to readAllStandard[Output/Error]

like image 194
Tyler Jandreau Avatar answered Oct 20 '22 16:10

Tyler Jandreau


What you want to execute is a shell command. You need to pass it to a shell. Run the following command using QProcess:

/bin/bash -c "your_command | with_pipes > and_redirects"
like image 31
Pavel Strakhov Avatar answered Oct 20 '22 16:10

Pavel Strakhov