Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Opening a PDF with openURL
Forum Updated to NodeBB v4.3 + New Features

Opening a PDF with openURL

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 1.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    David1246
    wrote on last edited by
    #1

    I am trying to open a PDF when a push button is pressed, I currently have the PDF added in the resources folder and I also updated the .qrc file. However when I try to print out the URL it prints out nothing.

    Screen Shot 2020-07-24 at 3.46.46 PM.png

    But when I give the full path of the PDF (without the colon) it is able to open.

    My question is how would you access files in the resources folder? In particular how would you open a PDF thats in the resources folder?

    Thanks in advance!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Your OS has no idea how to access files in Qt's resources. The usual way is to copy the file in a temporary one and open that copy.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply
      3
      • SGaistS SGaist

        Hi and welcome to devnet,

        Your OS has no idea how to access files in Qt's resources. The usual way is to copy the file in a temporary one and open that copy.

        D Offline
        D Offline
        David1246
        wrote on last edited by
        #3

        @SGaist Thanks for the reply!

        By copying the file to a temporary one do you mean copying the PDF to a local path with something like QFile::Copy?

        Is there any other way because I would prefer to not play around with the user's local machine.

        jsulmJ 1 Reply Last reply
        0
        • D David1246

          @SGaist Thanks for the reply!

          By copying the file to a temporary one do you mean copying the PDF to a local path with something like QFile::Copy?

          Is there any other way because I would prefer to not play around with the user's local machine.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @David1246 said in Opening a PDF with openURL:

          Is there any other way because

          Other way would be to not to put the PDF into a Qt resource, but bundle it with your app as normal file.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          D 1 Reply Last reply
          2
          • KH-219DesignK Offline
            KH-219DesignK Offline
            KH-219Design
            wrote on last edited by
            #5

            If you do ultimately choose to stream it to disk and open it from the disk file, then I shared a code snippet for that on a prior thread: https://forum.qt.io/topic/115682/how-to-get-the-path-of-a-c-file-which-is-added-as-the-resource-file/11

              // note the ":" at the front of the path
              std::string path_inside_qrc = ":/data/sim_rpc_replies_immediate_scores.pbtxt";
              std::unique_ptr<QTemporaryFile> temporary_file;
            
              temporary_file.reset(
                    QTemporaryFile::createNativeFile(QString::fromStdString(path_inside_qrc))
              );
              std::string file_name = temporary_file->fileName().toUtf8().constData();
              
              // TODO: add code to use the temporary file.
             
              // Delete the temp file after using it
              temporary_file.reset();
            

            www.219design.com
            Software | Electrical | Mechanical | Product Design

            1 Reply Last reply
            0
            • KH-219DesignK Offline
              KH-219DesignK Offline
              KH-219Design
              wrote on last edited by
              #6

              After my prior reply here, I got to thinking...

              Since the Qt method createNativeFile knows where to locate the bytes that it streams into the temporary on-disk file, we can go spelunking into the Qt implementation to see where those bytes are.

              That will take us an adventure through:

              • QFileDevice
              • QAbstractFileEngine
              • QResourceFileEngine
              • QResourceFileEngine::open
              • QResourceFileEnginePrivate::uncompress

              However... even then... knowing where the bytes are won't help you interact with QDesktopServices::openUrl.

              The openUrl function is documented as: "Opens the given url in the appropriate Web browser for the user's desktop environment"

              Since the file needs to be opened in a totally separate application than the Qt app that is invoking openUrl, it continues to be unworkable to use "qrc file bytes" that are compiled into the Qt app binary.

              You really do have to either create a temporary copy (like @SGaist said) or install the PDF file side-by-side with your application, but as a separate file on disk (not compiled in using qrc), as @jsulm said.

              www.219design.com
              Software | Electrical | Mechanical | Product Design

              D 1 Reply Last reply
              0
              • jsulmJ jsulm

                @David1246 said in Opening a PDF with openURL:

                Is there any other way because

                Other way would be to not to put the PDF into a Qt resource, but bundle it with your app as normal file.

                D Offline
                D Offline
                David1246
                wrote on last edited by David1246
                #7

                @jsulm
                I see thank you!

                One question I have is how would you bundle it with my app? Like in the cmake file?

                jsulmJ 1 Reply Last reply
                0
                • KH-219DesignK KH-219Design

                  After my prior reply here, I got to thinking...

                  Since the Qt method createNativeFile knows where to locate the bytes that it streams into the temporary on-disk file, we can go spelunking into the Qt implementation to see where those bytes are.

                  That will take us an adventure through:

                  • QFileDevice
                  • QAbstractFileEngine
                  • QResourceFileEngine
                  • QResourceFileEngine::open
                  • QResourceFileEnginePrivate::uncompress

                  However... even then... knowing where the bytes are won't help you interact with QDesktopServices::openUrl.

                  The openUrl function is documented as: "Opens the given url in the appropriate Web browser for the user's desktop environment"

                  Since the file needs to be opened in a totally separate application than the Qt app that is invoking openUrl, it continues to be unworkable to use "qrc file bytes" that are compiled into the Qt app binary.

                  You really do have to either create a temporary copy (like @SGaist said) or install the PDF file side-by-side with your application, but as a separate file on disk (not compiled in using qrc), as @jsulm said.

                  D Offline
                  D Offline
                  David1246
                  wrote on last edited by
                  #8

                  @KH-219Design
                  Thanks so much for the detailed response!! Ill try what you suggested and get back with the results

                  1 Reply Last reply
                  0
                  • D David1246

                    @jsulm
                    I see thank you!

                    One question I have is how would you bundle it with my app? Like in the cmake file?

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @David1246 See "Installing Files" in https://doc.qt.io/qt-5/qmake-advanced-usage.html
                    Or do you use CMake instead of qmake?

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved