Short Version
I have a resource file :/doc/foo.pdf that I'd like to open using QDesktopServices on OS X, Windows, and LINUX. I've tried various flavors of specifying the URL, e.g.,
url.setScheme("qrc");
url.setUrl(":/docs/foo.pdf");
QUrl url;
url.setScheme("qrc");
url.setUrl(":/docs/foo.pdf");
QDesktopServices::openUrl(url);
To copy to clipboard, switch view to plain text mode
but nothing seems to work.
Q: Is it possible to use QDesktopServices to open a resource file and, if so, how?
LONG VERSION
The following works on OS X:
void MyClass::on_menuActionOpenManual_triggered()
{
QUrl url
(directoryOf
("doc").
absoluteFilePath("foo.pdf"));
url.setScheme("file");
}
void MyClass::on_menuActionOpenManual_triggered()
{
QUrl url(directoryOf("doc").absoluteFilePath("foo.pdf"));
url.setScheme("file");
QDesktopServices::openUrl(url);
}
To copy to clipboard, switch view to plain text mode
where directoryOf() is defined as
{
#if defined(Q_OS_WIN)
if (dir.dirName().toLower() == "debug"
|| dir.dirName().toLower() == "release")
dir.cdUp();
#elif defined(Q_OS_MAC)
if (dir.dirName() == "MacOS") {
dir.cdUp();
dir.cdUp();
dir.cdUp();
}
#endif
dir.cd(subdir);
return dir;
}
QDir MyClass::directoryOf(const QString &subdir)
{
QDir dir(QApplication::applicationDirPath());
#if defined(Q_OS_WIN)
if (dir.dirName().toLower() == "debug"
|| dir.dirName().toLower() == "release")
dir.cdUp();
#elif defined(Q_OS_MAC)
if (dir.dirName() == "MacOS") {
dir.cdUp();
dir.cdUp();
dir.cdUp();
}
#endif
dir.cd(subdir);
return dir;
}
To copy to clipboard, switch view to plain text mode
This code was taken out of a Qt book, which is available online at http://www.informit.com/articles/art...05554&seqNum=2
The idea is to find the proper path to the containing directory with a switch on platforms. This technique works on Mac OS X but fails on Windows. Note that the relative directory on OS X is ../../../doc/. This led me to believe there has to be an easier way to locate resources and (if so) would potentially alleviate the trouble in displaying the PDF file in Windows. And yes, the test machine on Windows has a PDF viewer associated with .pdf file extensions.
Bookmarks