QDesktopServices::openUrl with Ressource
How do I open a ressource file (qressource
) using the command QDesktopServices::openUrl
?
I tried several ways, but n开发者_开发技巧one seemed to work (for instance QDesktopServices::openUrl(QUrl(tr(":ressource.pdf")));
)
Thank you.
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())));
}
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)
精彩评论