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. Resource path for non-qt functions
Forum Updated to NodeBB v4.3 + New Features

Resource path for non-qt functions

Scheduled Pinned Locked Moved Solved General and Desktop
29 Posts 6 Posters 4.7k Views 3 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.
  • E Engelard
    11 Aug 2020, 18:40

    @JonB why it is sound so complex, for such a simple thing as specifying file path...

    S Offline
    S Offline
    SGaist
    Lifetime Qt Champion
    wrote on 11 Aug 2020, 18:44 last edited by
    #5

    Hi,

    @Engelard said in Resource path for non-qt functions:

    @JonB why it is sound so complex, for such a simple thing as specifying file path...

    Qt resources are embedded in your executable/library. From the looks of it, you are trying to pass these files to some external library. That library has absolutely no idea on how to access the resources. Hence you will have to copy them somewhere on your hard drive to process it further.

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

    1 Reply Last reply
    1
    • E Engelard
      11 Aug 2020, 18:40

      @JonB why it is sound so complex, for such a simple thing as specifying file path...

      P Offline
      P Offline
      Pablo J. Rogina
      wrote on 11 Aug 2020, 18:45 last edited by
      #6

      @Engelard said in Resource path for non-qt functions:

      why it is sound so complex, for such a simple thing as specifying file path...

      You may want to read about the Qt Resource System which "is a platform-independent mechanism for storing binary files in the application's executable"

      You'll see then that it's just more than specifying a path...

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • J JonB
        11 Aug 2020, 18:44

        @Engelard
        :) It's not that. There isn't any physical file for that ":/myFiles/files/lego.jpg", is there? It's buried away in Qt resources, which only Qt can access, and it uses ":/..." to signify that.

        So you have to get it out to hand it to other programs (which you don't usually do). If you don't want to have to do the extraction, don't compile it in as a resource, supply it as an external file instead.

        E Offline
        E Offline
        Engelard
        wrote on 11 Aug 2020, 19:02 last edited by
        #7

        @JonB said in Resource path for non-qt functions:

        If you don't want to have to do the extraction, don't compile it in as a resource, supply it as an external file instead.

        Thats the first thought that i had, and i understand that i can't simply use resource path. But i also don't know where should i put my "files" folder with all my stuff for correct deployment, so my program will use it "externally", should i place it in project folder with my .pro .cpp .h files? Or should i put it build folder directly? And after that, how should i specify that relative path properly.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 11 Aug 2020, 19:03 last edited by
          #8

          Where are these files supposed to live ?
          What are they used for ?

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

          E 1 Reply Last reply 11 Aug 2020, 19:16
          0
          • S SGaist
            11 Aug 2020, 19:03

            Where are these files supposed to live ?
            What are they used for ?

            E Offline
            E Offline
            Engelard
            wrote on 11 Aug 2020, 19:16 last edited by Engelard 8 Nov 2020, 19:25
            #9

            @SGaist said in Resource path for non-qt functions:

            Where are these files supposed to live ?

            In program folder i suppose when it deployed and ready to use.

            @SGaist said in Resource path for non-qt functions:

            What are they used for ?

            For in-program calculations, image files mostly.

            1 Reply Last reply
            0
            • E Offline
              E Offline
              Engelard
              wrote on 11 Aug 2020, 19:27 last edited by
              #10

              If i want to use in my program relative path like:

              QString tempPath = "files/lego.jpg";
              

              where then i should place my physical folder "files" then?

              P J 2 Replies Last reply 11 Aug 2020, 19:33
              0
              • E Engelard
                11 Aug 2020, 19:27

                If i want to use in my program relative path like:

                QString tempPath = "files/lego.jpg";
                

                where then i should place my physical folder "files" then?

                P Offline
                P Offline
                Pablo J. Rogina
                wrote on 11 Aug 2020, 19:33 last edited by
                #11

                @Engelard said in Resource path for non-qt functions:

                where then i should place my physical folder "files" then?

                as a subfolder under the root (or installation) folder of your Qt app, i.e. relative to your Qt app executable

                /path/to/your/fantastic/app/myAppExecutable
                /path/to/your/fantastic/app/files
                /path/to/your/fantastic/app/files/myicon.jpg
                

                Upvote the answer(s) that helped you solve the issue
                Use "Topic Tools" button to mark your post as Solved
                Add screenshots via postimage.org
                Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                E 1 Reply Last reply 11 Aug 2020, 20:11
                3
                • E Engelard
                  11 Aug 2020, 19:27

                  If i want to use in my program relative path like:

                  QString tempPath = "files/lego.jpg";
                  

                  where then i should place my physical folder "files" then?

                  J Offline
                  J Offline
                  JonB
                  wrote on 11 Aug 2020, 19:39 last edited by JonB 8 Nov 2020, 19:43
                  #12

                  @Engelard
                  You can get into trouble using relative paths. If the current directory of your program is not what you think it is or changes, it may be wrong for you. When you open files at runtime you would be best making a full path to them. If you plan to pass this path to an external program, you may be advised to pass the full path. There are Qt methods to provide strings for the path, e.g. https://doc.qt.io/qt-5/qcoreapplication.html#applicationDirPath.

                  1 Reply Last reply
                  0
                  • P Pablo J. Rogina
                    11 Aug 2020, 19:33

                    @Engelard said in Resource path for non-qt functions:

                    where then i should place my physical folder "files" then?

                    as a subfolder under the root (or installation) folder of your Qt app, i.e. relative to your Qt app executable

                    /path/to/your/fantastic/app/myAppExecutable
                    /path/to/your/fantastic/app/files
                    /path/to/your/fantastic/app/files/myicon.jpg
                    
                    E Offline
                    E Offline
                    Engelard
                    wrote on 11 Aug 2020, 20:11 last edited by
                    #13

                    @Pablo-J-Rogina said in Resource path for non-qt functions:

                    as a subfolder under the root (or installation) folder of your Qt app, i.e. relative to your Qt app executable

                    So for the time i'm developing app i can place it to the build folder, right?
                    :D\Qt\build-testApp02-MSVC_for_x32-Debug\debug\files
                    But it is still a problem with specifying such relative path with string, i probably still writing it incorrectly:

                    QString tempPath = "/files/lego.jpg";
                    
                    1 Reply Last reply
                    0
                    • J JonB
                      11 Aug 2020, 18:38

                      @Engelard
                      You misunderstand.

                      ":/..." paths are Qt resources. They are available to Qt functionality. If you want to do anything "external" on them, it is your job to extract them to a physical file (use QFile) and pass that to the external thing to access.

                      E Offline
                      E Offline
                      Engelard
                      wrote on 11 Aug 2020, 21:00 last edited by
                      #14

                      @JonB I'm trying do as you firstly suggested with QFile, but i don't get what should i do. Get filepath with QFileInfo? but it is senseless, it simply give me back my path to /resources.

                      J 1 Reply Last reply 11 Aug 2020, 21:28
                      0
                      • E Engelard
                        11 Aug 2020, 21:00

                        @JonB I'm trying do as you firstly suggested with QFile, but i don't get what should i do. Get filepath with QFileInfo? but it is senseless, it simply give me back my path to /resources.

                        J Offline
                        J Offline
                        JonB
                        wrote on 11 Aug 2020, 21:28 last edited by JonB 8 Nov 2020, 21:28
                        #15

                        @Engelard
                        Just use QFile to open the ":/..." file for read, since it is QFile which understands this path syntax to mean read from resources. Write that to a physical, external file (also probably via a QFile) to export to file for outside world.

                        But a while ago I thought you were saying you were giving up on resource files and switching over to physical files anyway?

                        E 1 Reply Last reply 11 Aug 2020, 21:48
                        0
                        • J JonB
                          11 Aug 2020, 21:28

                          @Engelard
                          Just use QFile to open the ":/..." file for read, since it is QFile which understands this path syntax to mean read from resources. Write that to a physical, external file (also probably via a QFile) to export to file for outside world.

                          But a while ago I thought you were saying you were giving up on resource files and switching over to physical files anyway?

                          E Offline
                          E Offline
                          Engelard
                          wrote on 11 Aug 2020, 21:48 last edited by Engelard 8 Nov 2020, 21:51
                          #16

                          @JonB said in Resource path for non-qt functions:

                          Just use QFile to open the ":/..." file for read

                          I do not need read that, i need only it's path in string type so i could pass that as the parameter for OpenCV functions :) Which will read/write/anything to it.

                          @JonB said in Resource path for non-qt functions:

                          Write that to a physical, external file (also probably via a QFile) to export to file for outside world.

                          Seriously, i still don't get it, what exactly should i do with that? As far i understand that i should copy file from resources, paste it's copy somewhere in app's folder "buffered files or smth", then get absolutePath using QFileInfo and use that copy in the runtime of app?

                          @JonB said in Resource path for non-qt functions:

                          But a while ago I thought you were saying you were giving up on resource files and switching over to physical files anyway?

                          I did such stuff before, and i understand the simplicity of dumb folder creation, but never used QFiles and started to be interested, plus, tired of seeking answer what is correct way to write relative path in qt.

                          Ah, and QFileInfo gives zero info about the file path, same QString what i provided in the code(res directory), it gives in the runtime as a result :/myFiles/files/lego.jpg when this function absoluteFilePath() should return actual place of the file on the computer, but it does not.

                          J 1 Reply Last reply 11 Aug 2020, 22:16
                          0
                          • E Engelard
                            11 Aug 2020, 21:48

                            @JonB said in Resource path for non-qt functions:

                            Just use QFile to open the ":/..." file for read

                            I do not need read that, i need only it's path in string type so i could pass that as the parameter for OpenCV functions :) Which will read/write/anything to it.

                            @JonB said in Resource path for non-qt functions:

                            Write that to a physical, external file (also probably via a QFile) to export to file for outside world.

                            Seriously, i still don't get it, what exactly should i do with that? As far i understand that i should copy file from resources, paste it's copy somewhere in app's folder "buffered files or smth", then get absolutePath using QFileInfo and use that copy in the runtime of app?

                            @JonB said in Resource path for non-qt functions:

                            But a while ago I thought you were saying you were giving up on resource files and switching over to physical files anyway?

                            I did such stuff before, and i understand the simplicity of dumb folder creation, but never used QFiles and started to be interested, plus, tired of seeking answer what is correct way to write relative path in qt.

                            Ah, and QFileInfo gives zero info about the file path, same QString what i provided in the code(res directory), it gives in the runtime as a result :/myFiles/files/lego.jpg when this function absoluteFilePath() should return actual place of the file on the computer, but it does not.

                            J Offline
                            J Offline
                            JonB
                            wrote on 11 Aug 2020, 22:16 last edited by JonB
                            #17

                            @Engelard
                            I have explained, I don't know how to explain it any clearer.

                            If you ship a resource, it isn't an external file, you can't pass it to OpenCV anything.

                            A resource is addressed via :/... path. That is not a real file. The only thing that can open it is QFile.

                            Use that to open its content for read. Read its whole content, writing that to a real, external file. [EDIT @SimonSchroeder shows later on that you can simply use QFile::copy() to do this in one line.] Pass the path of that extracted file to your external OpenCV.

                            If you don't understand this, you can't use a resource file for your purpose of passing it to OpenCV. In that case give up on resources and supply the file as an external file, not a resource.

                            E 2 Replies Last reply 11 Aug 2020, 22:34
                            2
                            • J JonB
                              11 Aug 2020, 22:16

                              @Engelard
                              I have explained, I don't know how to explain it any clearer.

                              If you ship a resource, it isn't an external file, you can't pass it to OpenCV anything.

                              A resource is addressed via :/... path. That is not a real file. The only thing that can open it is QFile.

                              Use that to open its content for read. Read its whole content, writing that to a real, external file. [EDIT @SimonSchroeder shows later on that you can simply use QFile::copy() to do this in one line.] Pass the path of that extracted file to your external OpenCV.

                              If you don't understand this, you can't use a resource file for your purpose of passing it to OpenCV. In that case give up on resources and supply the file as an external file, not a resource.

                              E Offline
                              E Offline
                              Engelard
                              wrote on 11 Aug 2020, 22:34 last edited by
                              #18

                              @JonB said in Resource path for non-qt functions:

                              If you ship a resource, it isn't an external file, you can't pass it to OpenCV anything.

                              But how can it be? It exists somewhere in app folder... at least i was expecting that i could reach it. Thanks for explaining all this.

                              @JonB said in Resource path for non-qt functions:

                              In that case give up on resources and supply the file as an external file, not a resource.

                              seems it will be easier, but can you please clarify what is the right syntax for writing relative directories in qt? "/files/lego.jpg" is not working, and bunch of other ways i found in google.

                              J 1 Reply Last reply 11 Aug 2020, 22:43
                              0
                              • E Engelard
                                11 Aug 2020, 22:34

                                @JonB said in Resource path for non-qt functions:

                                If you ship a resource, it isn't an external file, you can't pass it to OpenCV anything.

                                But how can it be? It exists somewhere in app folder... at least i was expecting that i could reach it. Thanks for explaining all this.

                                @JonB said in Resource path for non-qt functions:

                                In that case give up on resources and supply the file as an external file, not a resource.

                                seems it will be easier, but can you please clarify what is the right syntax for writing relative directories in qt? "/files/lego.jpg" is not working, and bunch of other ways i found in google.

                                J Offline
                                J Offline
                                JonB
                                wrote on 11 Aug 2020, 22:43 last edited by JonB 8 Nov 2020, 22:45
                                #19

                                @Engelard
                                /files/lego.jpg is not a relative path. It starts with a /, so it's absolute.

                                Relative paths do not start with /. lego.jpg, files/lego.jpg and ../lego.jpg are all relative paths. They are relative to whatever the current directory is.

                                These relative/absolutes have nothing to do with Qt. The only "special" path in Qt is :/..., which Qt alone treats as referring to a file in its resources, not accessible to the outside world.

                                E 1 Reply Last reply 11 Aug 2020, 22:47
                                1
                                • J JonB
                                  11 Aug 2020, 22:43

                                  @Engelard
                                  /files/lego.jpg is not a relative path. It starts with a /, so it's absolute.

                                  Relative paths do not start with /. lego.jpg, files/lego.jpg and ../lego.jpg are all relative paths. They are relative to whatever the current directory is.

                                  These relative/absolutes have nothing to do with Qt. The only "special" path in Qt is :/..., which Qt alone treats as referring to a file in its resources, not accessible to the outside world.

                                  E Offline
                                  E Offline
                                  Engelard
                                  wrote on 11 Aug 2020, 22:47 last edited by
                                  #20

                                  @JonB said in Resource path for non-qt functions:

                                  They are relative to whatever the current directory is.

                                  okay, so folder "files" is relative to my .exe and other stuff in the debug folder of my project, but none

                                  "../files/lego.jpg"
                                  

                                  or

                                  "files/lego.jpg"
                                  

                                  is not working...

                                  P.S. how do you highlight some text with red in your posts?)

                                  1 Reply Last reply
                                  0
                                  • J JonB
                                    11 Aug 2020, 22:16

                                    @Engelard
                                    I have explained, I don't know how to explain it any clearer.

                                    If you ship a resource, it isn't an external file, you can't pass it to OpenCV anything.

                                    A resource is addressed via :/... path. That is not a real file. The only thing that can open it is QFile.

                                    Use that to open its content for read. Read its whole content, writing that to a real, external file. [EDIT @SimonSchroeder shows later on that you can simply use QFile::copy() to do this in one line.] Pass the path of that extracted file to your external OpenCV.

                                    If you don't understand this, you can't use a resource file for your purpose of passing it to OpenCV. In that case give up on resources and supply the file as an external file, not a resource.

                                    E Offline
                                    E Offline
                                    Engelard
                                    wrote on 11 Aug 2020, 23:03 last edited by
                                    #21

                                    @JonB said in Resource path for non-qt functions:

                                    Read its whole content, writing that to a real, external file.

                                    you mean - make a copy of that file from resources? Even if i'll do that, i should create folder for that .jpg file, so it would'nt be messy, and then i should delete that copy when my program is about to be closed. Am i right?

                                    1 Reply Last reply
                                    0
                                    • E Offline
                                      E Offline
                                      Engelard
                                      wrote on 11 Aug 2020, 23:54 last edited by
                                      #22

                                      Problem "resolved". From this topic i understand that i can't acces .res files of my qt app, it's okay. But then i found that i'm not able to simply use relative path's in my app for some strange qt reason.

                                      So i found such wonderful static function - QString QCoreApplication::applicationDirPath(); which returns directory of running app, then simply add desired "relative" path QString and here we go!

                                      J 1 Reply Last reply 12 Aug 2020, 07:35
                                      0
                                      • B Offline
                                        B Offline
                                        Bonnie
                                        wrote on 11 Aug 2020, 23:55 last edited by Bonnie 8 Dec 2020, 00:06
                                        #23

                                        What OpenCV functions are you planning to use with the image files?
                                        If you only use cv::imread you can try to replace that with QFile + cv::imdecode or create a cv::Mat directly from a QImage.

                                        E 1 Reply Last reply 12 Aug 2020, 00:08
                                        2
                                        • B Bonnie
                                          11 Aug 2020, 23:55

                                          What OpenCV functions are you planning to use with the image files?
                                          If you only use cv::imread you can try to replace that with QFile + cv::imdecode or create a cv::Mat directly from a QImage.

                                          E Offline
                                          E Offline
                                          Engelard
                                          wrote on 12 Aug 2020, 00:08 last edited by Engelard 8 Dec 2020, 00:20
                                          #24

                                          @Bonnie said in Resource path for non-qt functions:

                                          or create a cv::Mat directly from a QImage.

                                          Tnx. I just started with OpenCV, by now need exactly Mat creation. Which one is better for performance? From QFile or from QImage?

                                          1 Reply Last reply
                                          0

                                          14/29

                                          11 Aug 2020, 21:00

                                          • Login

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