Start a process using QProcess

a 夏天 提交于 2019-11-29 00:30:35
mosg

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

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.

Narasimha Reddy MV
QProcess *pro = new QProcess;
QString s = "\"C:\Users\xyz\Desktop\Example.exe";
pro ->start(s);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!