开发者

Start a process using QProcess

开发者 https://www.devze.com 2022-12-26 06:46 出处:网络
I\'m trying to start Microsoft word using QProcess as following: QString program = \"WINWORD.EXE\"; process->start(program);

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 ?


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


For me, I need to add " characteres :

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


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


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.


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消