Klaus
Klaus

Reputation: 1251

QDesktopServices::openUrl with Ressource

How do I open a ressource file (qressource) using the command QDesktopServices::openUrl ?

I tried several ways, but none seemed to work (for instance QDesktopServices::openUrl(QUrl(tr(":ressource.pdf")));)

Thank you.

Upvotes: 1

Views: 2405

Answers (2)

Vereb
Vereb

Reputation: 14716

Unfortunately you can't do it directly, save it to a file first.

I check the Qt source. This is because the url is passed to the browser or other application (depending on the protocol) directly. These applications will not see your resource because thay are in a different process.

Here is the related source:

qdesktopservices.cpp:


bool QDesktopServices::openUrl(const QUrl &url)
{
   ...
}

qdesktopservices_x11.cpp:


static bool openDocument(const QUrl &url)
{
    ...
}

static bool launchWebBrowser(const QUrl &url)
{
   ...
}

inline static bool launch(const QUrl &url, const QString &client)
{  
    return (QProcess::startDetached(client + QLatin1Char(' ') + QString::fromLatin1(url.toEncoded().constData())));  
}

Upvotes: 6

Andreas Wallner
Andreas Wallner

Reputation: 9

You shouldn't need to open the resource files if they are added to your qmake project file correctly:

RESOURCES += resources.qrc

Then you should be able to use the files included in your resource file via the syntax you used above:

:/path/filename

(Path and filename inside the resource file)

FYI: QDesktopServices::openUrl is used to open the standard browser with a specific webpage. And, you shouldn't use tr("") on path names etc. only on text displayed to a user (which should be translated for multilingual applications)

Upvotes: 0

Related Questions