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. Is it possible to get the absolute file path of the resource file? [SOLVED]

Is it possible to get the absolute file path of the resource file? [SOLVED]

Scheduled Pinned Locked Moved General and Desktop
qt5
7 Posts 3 Posters 8.9k Views
  • 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.
  • sosunS Offline
    sosunS Offline
    sosun
    wrote on last edited by sosun
    #1

    I have a resource file which contains a few images. I'm using an external library which asks for path to the file in its constructor. I need to give it to the constructor, but I don't know how can I extract the path from resource file, not the :/images/bubble.jpg, but /home/john/Pictures/bubble.jpg. I tried to use the next variant:

    QFileInfo image(":/images/bubble.jpg");
    QString imagePath = image.absoluteFilePath();
    

    But it returns the resource file path - :/images/blah-blah.

    Maybe it's possible to create folder within the project and use something like:

    QString imagePath = "$PROJECT_DIR" + "/images/bubble.png"
    

    But I couldn't find the appropriate variable. I tried to use getenv() as well as qgetenv - got nothing. I know there's a qmake variable called $$_PRO_FILE_PWD_ which returns the folder with .pro file, but I didn't figure out how to use qmake variable.

    What can be the solution to this problem? Not necessarily that it would be a completely different one, it's ok. I just need to be able to have a folder within the project where I can store images and get the absolute path of each file.

    T mrjjM 2 Replies Last reply
    0
    • sosunS sosun

      I have a resource file which contains a few images. I'm using an external library which asks for path to the file in its constructor. I need to give it to the constructor, but I don't know how can I extract the path from resource file, not the :/images/bubble.jpg, but /home/john/Pictures/bubble.jpg. I tried to use the next variant:

      QFileInfo image(":/images/bubble.jpg");
      QString imagePath = image.absoluteFilePath();
      

      But it returns the resource file path - :/images/blah-blah.

      Maybe it's possible to create folder within the project and use something like:

      QString imagePath = "$PROJECT_DIR" + "/images/bubble.png"
      

      But I couldn't find the appropriate variable. I tried to use getenv() as well as qgetenv - got nothing. I know there's a qmake variable called $$_PRO_FILE_PWD_ which returns the folder with .pro file, but I didn't figure out how to use qmake variable.

      What can be the solution to this problem? Not necessarily that it would be a completely different one, it's ok. I just need to be able to have a folder within the project where I can store images and get the absolute path of each file.

      T Offline
      T Offline
      TioRoy
      wrote on last edited by
      #2

      @sosun,

      Qt resource "is mechanism for storing binary files in the application's executable". So you don't have the file in your file system after you deploy your application. Only in development they exists.

      sosunS 1 Reply Last reply
      0
      • T TioRoy

        @sosun,

        Qt resource "is mechanism for storing binary files in the application's executable". So you don't have the file in your file system after you deploy your application. Only in development they exists.

        sosunS Offline
        sosunS Offline
        sosun
        wrote on last edited by
        #3

        @TioRoy So, is there anything I can do in my situation?

        T 1 Reply Last reply
        0
        • sosunS sosun

          I have a resource file which contains a few images. I'm using an external library which asks for path to the file in its constructor. I need to give it to the constructor, but I don't know how can I extract the path from resource file, not the :/images/bubble.jpg, but /home/john/Pictures/bubble.jpg. I tried to use the next variant:

          QFileInfo image(":/images/bubble.jpg");
          QString imagePath = image.absoluteFilePath();
          

          But it returns the resource file path - :/images/blah-blah.

          Maybe it's possible to create folder within the project and use something like:

          QString imagePath = "$PROJECT_DIR" + "/images/bubble.png"
          

          But I couldn't find the appropriate variable. I tried to use getenv() as well as qgetenv - got nothing. I know there's a qmake variable called $$_PRO_FILE_PWD_ which returns the folder with .pro file, but I didn't figure out how to use qmake variable.

          What can be the solution to this problem? Not necessarily that it would be a completely different one, it's ok. I just need to be able to have a folder within the project where I can store images and get the absolute path of each file.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @sosun
          hmm, the resource file is compiled into the application.
          So are you sure an external library can load it from the exe ressources ?

          QCoreApplication::applicationDirPath()
          can tell where you exe is run from and you can construct a full path to image folder using that.
          Please notice that you will need a copy of the image folder to the build directory as that is where your exe is being run from until deployment.

          also see
          http://stackoverflow.com/questions/23844136/getting-paths-of-qrc-files-in-qt

          sosunS 1 Reply Last reply
          1
          • sosunS sosun

            @TioRoy So, is there anything I can do in my situation?

            T Offline
            T Offline
            TioRoy
            wrote on last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • mrjjM mrjj

              @sosun
              hmm, the resource file is compiled into the application.
              So are you sure an external library can load it from the exe ressources ?

              QCoreApplication::applicationDirPath()
              can tell where you exe is run from and you can construct a full path to image folder using that.
              Please notice that you will need a copy of the image folder to the build directory as that is where your exe is being run from until deployment.

              also see
              http://stackoverflow.com/questions/23844136/getting-paths-of-qrc-files-in-qt

              sosunS Offline
              sosunS Offline
              sosun
              wrote on last edited by sosun
              #6

              @mrjj Yeah, it works! Also, in the release version it will seek for the image not in the build dir, but in the primary folder of the project, am I right?

              mrjjM 1 Reply Last reply
              0
              • sosunS sosun

                @mrjj Yeah, it works! Also, in the release version it will seek for the image not in the build dir, but in the primary folder of the project, am I right?

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @sosun
                well if run from inside the IDE / creator, and it is set to release mode, it will run from the release folder. as far as i know. just like the debug folder.

                If you copy the exe file to another folder and run it there , then it will be that folder.

                1 Reply Last reply
                1

                • Login

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