Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a windows cmd command using Qt?

Tags:

c++

qt

qprocess

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>");
like image 291
Lasitha Konara Avatar asked Nov 23 '15 07:11

Lasitha Konara


2 Answers

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
}
like image 133
Ilya Avatar answered Oct 21 '22 10:10

Ilya


I solved my problem using following simple code segment

#include <QDir>

QDir::setCurrent("D:/MyWork/Temp/source");
system("git gui");
like image 38
Lasitha Konara Avatar answered Oct 21 '22 10:10

Lasitha Konara