-2

In my C++/Qt program I would like to know which text editor is available on user's Linux machine so that I can use it to open a text file from my program. There are several widely used editors such as gedit, kate and a few more so I would like to check these. My naive intention was to use QProcess in combinations with command -v foobar as in How to check if a program exists from a Bash script? but unfortunately this does not seem to be doing what I expect. I hoped that the exit codes could indicate the result.

qDebug() << QProcess::execute("command -v kate") << QProcess::execute("command -v gedit");

... but the processes did not even started and the line returned -2 -2.

Is there any way to achieve what I need?

1 Answers1

1

You need to run it through sh, setProcessChannelMode to get error of the child of sh command :

QProcess chkexists;
chkexists.setProcessChannelMode(QProcess::MergedChannels);
chkexists.start("sh", QStringList()<<"-c"<<"command -v kate");
chkexists.waitForFinished();
if(chkexists.exitCode()==0){
////editor exists
}

Soheil Armin
  • 2,725
  • 1
  • 6
  • 18