7

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>");
Ilya
  • 4,583
  • 4
  • 26
  • 51
Lasitha Konara
  • 1,111
  • 3
  • 12
  • 19

6 Answers6

3

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
}
Ilya
  • 4,583
  • 4
  • 26
  • 51
  • I tried all these,but still my GUI does not pop up. QProcess process; process.setWorkingDirectory("D:\\MyWork\\Temp\\source"); process.start("git gui"); – Lasitha Konara Nov 23 '15 at 08:19
  • 1) Could you please check it again, but with full path to git.exe? Something like `QProcess::startDetached("C:\\Users\\\\AppData\\Local\\GitHub\\PortableGit_\\bin\\git.exe", QStringList() << "gui", "D:\\MyWork\\Temp\\source");`? (please set correct path!) 2) Thank you for pointing out on this, I fixed this mistake. – Ilya Nov 23 '15 at 08:35
  • I tried that too with correct paths of the exe ,still its not working,Can't figure out the reason – Lasitha Konara Nov 23 '15 at 09:44
  • So, try to run something else (like "notepad.exe") the same way. Do it just to become sure that this code could run at least something on your side (on my side everything works). Also please check that you have quotes around the path in case of spaces in it (something like `"\"c:\\path with spaces\\prog.exe\"`). – Ilya Nov 23 '15 at 10:28
3

I solved my problem using following simple code segment

#include <QDir>

QDir::setCurrent("D:/MyWork/Temp/source");
system("git gui");
Lasitha Konara
  • 1,111
  • 3
  • 12
  • 19
2

Even if you're using Qt, you can still call Windows API. ShellExecute will do this job

#include <Windows.h>
ShellExecute(NULL, NULL, "git", "gui", NULL, SW_SHOWNORMAL);

And if your charset is Unicode (Wide Char), try following code

#include <Windows.h>
ShellExecute(NULL, NULL, _T("git"), _T("gui"), NULL, SW_SHOWNORMAL);
John
  • 96
  • 7
1

You don't need to worry about the separator, Qt will take care of that for you.

See QDir Document

You do not need to use this function to build file paths. If you always use "/", Qt will translate your paths to conform to the underlying operating system. If you want to display paths to the user using their operating system's separator use toNativeSeparators().

For your QProcess, try this.

QProcess gitProcess;
gitProcess.setWorkingDirectory("D:/MyWork/Temp/source");
gitProcess.setProgram("git"); // hope this is in your PATH
gitProcess.setArguments(QStringList() << "gui");
gitProcess.start();
if (gitProcess.waitForStarted()) {
  // Now your app is running.
}
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
ramtheconqueror
  • 1,907
  • 1
  • 22
  • 35
1

I know this post is kinda old now but I know what's wrong with Ilya's answer (to my understanding). Since the QProcess is created within a local scope, whenever you call a process outside of the scope the destructor will be automatically called, and kill the process along the way, as the debug message implies:

Destroyed while process ("git") is still running.

One way to fix this is by actually dynamically allocate an instance of QProcess on the heap. Make sure to free the memory after the process is finished.

QProcess* process = new QProcess;
process->setWorkingDirectory("D:\\MyWork\\Temp\\source");
process->start("git", QStringList() << "gui");

Or to wait until the process is finished, using

process.waitForFinished (-1);

I hope this will help for anyone looking for the right answer in this post.

SalvagedDoor
  • 21
  • 1
  • 5
0

Instead of using system() do this so you can stay within the QT framework:

QDir::setCurrent("D:/MyWork/Temp/source");
myProcess.startDetached("git gui");