How to run a windows cmd command using Qt?

Lasitha Konara picture Lasitha Konara · Nov 23, 2015 · Viewed 11.9k times · Source

I have to run the following command using Qt, which will pop up the Git GUI window.

D:\MyWork\Temp\source>git gui

How do I do that?

I tried the following, but it didn't work:

QProcess process;   
process.start("git gui",QStringList() << "D:\MyWork\Temp\source>");

Answer

Ilya picture Ilya · Nov 23, 2015

Try this:

QProcess process;
process.setWorkingDirectory("D:\\MyWork\\Temp\\source");
process.start("git", QStringList() << "gui");

Or if you want to do it in one line, you can do this (here we are using startDetached instead of start):

QProcess::startDetached("git", QStringList() << "gui", "D:\\MyWork\\Temp\\source");

In the second case it is better to check the return code (to show error message if your program can't run external program). Also you can put all the arguments in the first program string (i.e. process.start("git gui"); is allowed too):

bool res = QProcess::startDetached("git gui", QStringList(), "D:\\MyWork\\Temp\\source");
if (!res) {
  // show error message
}