How to run cmd command using Qt?

I need to run the following command with Qt, which will open a Git GUI window.

D:\MyWork\Temp\source>git gui 

How to do it?

I tried the following, but that did not work:

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

I solved my problem using the following simple code segment

 #include <QDir> QDir::setCurrent("D:/MyWork/Temp/source"); system("git gui"); 
+2
source

Try the following:

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

Or if you want to do this on one line, you can do it (here we use 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 an error message if your program cannot start an external program). You can also put all arguments in the first line of program (i.e. process.start("git gui"); too):

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

Even if you use Qt, you can still call the Windows API. ShellExecute will complete this task.

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

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

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

You do not need to worry about the delimiter, Qt will take care of this for you.

See QDir Document

You do not need to use this function to create file paths. if you always use "/", Qt will translate your paths to match the underlying operating system. If you want to display user paths using their operating system 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. } 
+1
source

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

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

All Articles