How to run a windows cmd command using Qt?

13,390

Solution 1

I solved my problem using following simple code segment

#include <QDir>

QDir::setCurrent("D:/MyWork/Temp/source");
system("git gui");

Solution 2

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
}

Solution 3

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);

Solution 4

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.
}

Solution 5

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.

Share:
13,390
Lasitha Konara
Author by

Lasitha Konara

Updated on June 17, 2022

Comments

  • Lasitha Konara
    Lasitha Konara almost 2 years

    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>");