kaycee
kaycee

Reputation: 125

Start a process using QProcess

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 ?

Upvotes: 9

Views: 61503

Answers (6)

miko53
miko53

Reputation: 91

For me, I need to add " characteres :

m_process->start("\"C:\\Program Files (x86)\\Notepad++\\notepad++.exe\"");

Upvotes: 6

Narasimha Reddy MV
Narasimha Reddy MV

Reputation: 11

QProcess *pro = new QProcess;
QString s = "\"C:\Users\xyz\Desktop\Example.exe";
pro ->start(s);

Upvotes: 0

mosg
mosg

Reputation: 12391

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

Upvotes: 18

Gilco
Gilco

Reputation: 1336

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.

Upvotes: 0

Georgii Iesaulov
Georgii Iesaulov

Reputation: 69

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.

Upvotes: 1

chalup
chalup

Reputation: 8516

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().

Upvotes: 2

Related Questions