PDA

View Full Version : Open file in its standard application, according to file extension



Boron
16th February 2010, 14:58
Hello,
I just wanted to to "start" a html file, expecting it to be displayed in the standard browser (Firefox in my case; may be Internet Explorer or anything else on other systems; my application is restricted to Windows systems for other reasons):

QString command(QCoreApplication::applicationDirPath() + "/Help/index.html" );
bool success = QProcess::startDetached( command );

The html file is in the subdirectory "Help" that lies parallel to the executable.

On Windows systems "starting" a file of any type normally starts the application that is linked to the file extension.
When trying to use this behaviour with a QProcess it doesn't seem to work.

Any ideas how I can open a file in its "standard application"?

spirit
16th February 2010, 15:03
take a look at QDesktopServices::openUrl.

Boron
16th February 2010, 15:04
I just found out that the following code will do the job:

QDesktopServices::openUrl ( QCoreApplication::applicationDirPath() + "/Help/index.html" );
I seems to work not only for html files but also for any other files (bmp, txt etc.).

spirit
16th February 2010, 15:10
it's better to use QDesktopServices::openUrl with QUrl::fromLocalFile.

Boron
16th February 2010, 17:42
I will add the fromLocalFile() call. Thanks for the hint.