8

I'm trying to start Microsoft word using QProcess as following:

QString program = "WINWORD.EXE";
process->start(program);

but nothing happens. winword.exe is on path (so when i type winword.exe word is openning up). Is it the right way to do so ?

DigviJay Patil
  • 986
  • 14
  • 31
kaycee
  • 115
  • 1
  • 2
  • 5

6 Answers6

18

may be code below will help you:

QProcess *process = new QProcess(this);
QString program = "explorer.exe";
QString folder = "C:\\";
process->start(program, QStringList() << folder);

I think you are trying to execute program that doesn't consists in global $PATH windows variable, that's why winword.exe doesn't executes.

Also you may need to define absolute path to program, e.g.:

QString wordPath = "C:\\Program Files\\Microsoft Office\\Office12\\WINWORD.EXE"
process->start(wordPath, QStringList() << "");
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
mosg
  • 12,041
  • 12
  • 65
  • 87
  • Why is there is there an empty "" string as the second argument for QStringList()? I must put these or my exe won't open, but I don't know why. – Zebrafish Dec 27 '17 at 09:42
  • @Zebrafish It's an arguments. "" means empty arguments. – mosg Jan 22 '18 at 10:31
  • It's because if you use the single argument overload and the path includes a space, the chars after the space are taken to be parameters or arguments passed to the executable. Using the two argument overload with an empty string makes it specific that the first argument is the complete path of the executable, even if it contains spaces, and the arguments are strictly passed as its second argument. Confusing, I know. – Zebrafish Jan 22 '18 at 11:53
6

For me, I need to add " characteres :

m_process->start("\"C:\\Program Files (x86)\\Notepad++\\notepad++.exe\"");
Elvis Dukaj
  • 7,142
  • 12
  • 43
  • 85
miko53
  • 91
  • 1
  • 4
1

If the method, where you're trying to launch external process, is finished right after your code, e.g.:

void foo() {
    ...
    QString program = "WINWORD.EXE";
    process->start(program);
}

and variable

process

was declared as local variable, it will be destroyed at the end of method and no external process will be started - or correctly you won't see it because it will be destroyed right after start.

It was the reason for similar issue in my case. Hope it helps.

1

From Qt documentation:

Note: Processes are started asynchronously, which means the started() and error() signals may be delayed. Call waitForStarted() to make sure the process has started (or has failed to start) and those signals have been emitted.

Connect the signals mentioned in doc to some GUI control or debug output and see what happens. If there is an error, you should check the error type using QProcess::error().

chalup
  • 8,358
  • 3
  • 33
  • 38
0

You can just set the working directory:

myProcess = new QProcess();
myProcess->setWorkingDirectory("C:\\Z-Programming_Source\\Java-workspace\\Encrypt1\\bin\\");

Or do it at start:

myProcess->start("dir \"My Documents\"");

At start() you can enter a command for the console... read the manual.

I prefer the first option. More readable.

Gilco
  • 1,326
  • 12
  • 13
0
QProcess *pro = new QProcess;
QString s = "\"C:\Users\xyz\Desktop\Example.exe";
pro ->start(s);
scopchanov
  • 7,966
  • 10
  • 40
  • 68
  • 1
    While this might answer the authors question, it lacks some explaining words and links to documentation. Raw code snippets are not very helpful without some phrases around it. You may also find [how to write a good answer](https://stackoverflow.com/help/how-to-answer) very helpful. Please edit your answer. – hellow Sep 21 '18 at 11:12