Results 1 to 2 of 2

Thread: QDesktopServices to open resource file?

  1. #1
    Join Date
    Nov 2009
    Posts
    20
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question QDesktopServices to open resource file?

    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.,

    Qt Code:
    1. QUrl url;
    2. url.setScheme("qrc");
    3. url.setUrl(":/docs/foo.pdf");
    4. 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:

    Qt Code:
    1. void MyClass::on_menuActionOpenManual_triggered()
    2. {
    3. QUrl url(directoryOf("doc").absoluteFilePath("foo.pdf"));
    4. url.setScheme("file");
    5. QDesktopServices::openUrl(url);
    6. }
    To copy to clipboard, switch view to plain text mode 

    where directoryOf() is defined as

    Qt Code:
    1. QDir MyClass::directoryOf(const QString &subdir)
    2. {
    3. QDir dir(QApplication::applicationDirPath());
    4.  
    5. #if defined(Q_OS_WIN)
    6. if (dir.dirName().toLower() == "debug"
    7. || dir.dirName().toLower() == "release")
    8. dir.cdUp();
    9. #elif defined(Q_OS_MAC)
    10. if (dir.dirName() == "MacOS") {
    11. dir.cdUp();
    12. dir.cdUp();
    13. dir.cdUp();
    14. }
    15. #endif
    16. dir.cd(subdir);
    17. return dir;
    18. }
    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: QDesktopServices to open resource file?

    It is not possible because the resource file does not exist on the filesystem outside of your application. The resources are a virtual file system that only exists within your application. You have to copy the file first to some temp location in the real filesystem.
    It's nice to be important but it's more important to be nice.

Similar Threads

  1. QDesktopServices open url & local file & pdf or doc
    By patrik08 in forum Qt Programming
    Replies: 9
    Last Post: 14th April 2012, 04:42
  2. Open file without run again the program
    By avis_phoenix in forum Qt Programming
    Replies: 1
    Last Post: 7th August 2009, 23:01
  3. How to include qrc resource file in Visual Studio 2005?
    By jambrek in forum Qt Programming
    Replies: 18
    Last Post: 8th February 2008, 18:26
  4. Client/Server Error: BadIDChoice
    By 3nc31 in forum Qt Programming
    Replies: 5
    Last Post: 27th November 2007, 10:22
  5. How to open a Pdf file from Qt
    By vishal.chauhan in forum Newbie
    Replies: 2
    Last Post: 28th March 2007, 08:24

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.