Run the shell command through QProcess on the Android platform

I cannot run any command through QProcess on Android platform. I am using the Qt library. Can anyone explain how to run shell commands from my Android app?

QProcess process(); process.execute("ls"); bool finished = process.waitForFinished(-1); qDebug() << "End : " << finished << " Output : " << process.errorString(); 

The process does not end unless I specify a timeout. process.waitForFinished () returns false when I specify a timeout of, say, 10,000 ms.

+7
android qt qprocess
source share
2 answers

Your sample code is defective and it will not work on ANY platform! The ls not valid! This command is built into a shell program such as bash .

Another error of your code is that QProcess::execute is a static function . Thus, the final line does not affect the process that you tried to start.

So your code should look like this:

 QProcess process; process.start("bash", QStringList() << "-c" << "ls"); bool finished = process.waitForFinished(-1); 
+5
source share

You are using QProcess::execute() , which is a static function. Quoting Qt Documentation : "Runs a program command in a new process, waits for it to complete."

So what could happen in your code:

 QProcess process(); process.execute("ls"); // Start "ls" and wait for it to finish // "ls" has finished bool finished = process.waitForFinished(-1); // Wait for the process to finish, but there is no process and you could get locked here forever... 

There are two ways to fix your code:

 QProcess process(); process.start("ls"); // Start "ls" and returns bool finished = process.waitForFinished(-1); qDebug() << "End : " << finished << " Output : " << process.errorString(); 

or

  QProcess::execute("ls"); 
0
source share

All Articles