PDA

View Full Version : How to detach a QProcess?



d_stranz
27th August 2015, 23:39
I have created HTML help for my app (using the Helpinator product). I use this code to fir it off from a menu action:



void MainWindow::onHelpUserManual()
{
QString helpIndexUrl = QCoreApplication::applicationDirPath() + "/htmlhelp/index.html";
int result = QProcess::execute( "cmd.exe", QStringList() << "/c" << helpIndexUrl );
if ( result < 0 )
QMessageBox::warning( this, "Command failed", "Could not open user manual. Please open it using the Windows Start menu instead." );
}


This works fine - if my browser (Firefox) is running, it opens a new tab and loads the help file into it. If it isn't running, it starts the browser and loads the help file.

The problem is this: In the first case (browser already running), the call to QProcess::execute() returns immediately. In the second case (browser not running), the execute call does not return and so the program is frozen until I exit the browser.

(Pause while I close the browser and test QProcess::startDetached())

startDetached() does not work either. Bizarre behavior - in a debug build, startDetached() will run up a command window, which then starts the browser. In a release build, startDetached() results in a quick flash of the command window, then nothing. Doesn't matter whether the browser is running or not.

How do I do this? I am at a loss to explain what I am observing.

anda_skoa
28th August 2015, 07:57
startDetached() should work.

Any reason you are launching this command instead of using QDesktopServices::openUrl()?

Cheers,
_

d_stranz
28th August 2015, 14:58
Any reason you are launching this command instead of using QDesktopServices:: openUrl()?

Lack of knowledge :confused: This sounds like a much better solution - I looked for something like this, but didn't know the right keywords.

-- Edit - works like a charm! Thanks!

Anyway, my brain woke me early this morning to tell me that if you want to open a file, you should make sure the file actually exists in the place where you are trying to open it. I had made a copy of the html help file in the debug output directory, but not in the release directory.

Frdz
28th August 2015, 22:20
Any reason you are launching this command instead of using QDesktopServices:penUrl()?

Ouch, I was tought this class was fully deprecated, therefore I didn't read if there's some members still can be used. Thank you for having me realize it :D