Reputation: 1201
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>");
Upvotes: 8
Views: 17780
Reputation: 21
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 kills 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 allocating 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.
Upvotes: 1
Reputation: 1964
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.
}
Upvotes: 1
Reputation: 99
Instead of using system() do this so you can stay within the QT framework:
QDir::setCurrent("D:/MyWork/Temp/source");
myProcess.startDetached("git gui");
Upvotes: 0
Reputation: 1201
I solved my problem using following simple code segment
#include <QDir>
QDir::setCurrent("D:/MyWork/Temp/source");
system("git gui");
Upvotes: 4
Reputation: 86
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);
Upvotes: 2
Reputation: 4689
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
}
Upvotes: 3