Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Quick question about loading images

Quick question about loading images

Scheduled Pinned Locked Moved Unsolved Qt for Python
9 Posts 5 Posters 559 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.
  • P Offline
    P Offline
    pablomichea
    wrote on 12 May 2022, 07:04 last edited by
    #1

    I'm trying to load an image using the path:

    image = 'images/world.png'

    However it doesn't work, it only works if i use an absolute path. Everything in the right place, folder and image but it doesn't work with a relative path.

    Ok thanks world!!

    J 1 Reply Last reply 12 May 2022, 08:17
    0
    • P pablomichea
      12 May 2022, 07:04

      I'm trying to load an image using the path:

      image = 'images/world.png'

      However it doesn't work, it only works if i use an absolute path. Everything in the right place, folder and image but it doesn't work with a relative path.

      Ok thanks world!!

      J Offline
      J Offline
      JonB
      wrote on 12 May 2022, 08:17 last edited by JonB 5 Dec 2022, 08:20
      #2

      @pablomichea said in Quick question about loading images:

      However it doesn't work, it only works if i use an absolute path. Everything in the right place, folder and image but it doesn't work with a relative path.

      Precisely! What do you expect your path to be relative to?!

      It's relative to the current working directory of your application. Which is... what? You don't know, and depends how the user invokes your application.

      You simply should never rely on the current working directory being anything specific and hence you should never use relative paths in your code (unless you know what you are doing!). Use absolute paths to access files. And utilise Qt's QStandardPaths Class to build absolute paths from suitable standard locations at run-time. You can alternatively use Qt's resource paths to access resources stored inside the executable if you wish to put them there.

      1 Reply Last reply
      2
      • P Offline
        P Offline
        pablomichea
        wrote on 12 May 2022, 20:50 last edited by
        #3

        Thanks you JonB, i understand what you mean, i just wanted to know why it won't work. I'm currently studying the book "Beginning PyQt" (2nd edition) and well.. they used a relative path but it doesn't work for me, it just work with an absolute path.

        Thanks!!

        J 1 Reply Last reply 12 May 2022, 22:22
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 12 May 2022, 21:02 last edited by
          #4

          Hi,

          Where is your script ?
          Where is that folder with regards to your script ?
          Where are you calling your script from ?

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

          P 1 Reply Last reply 12 May 2022, 21:26
          0
          • S SGaist
            12 May 2022, 21:02

            Hi,

            Where is your script ?
            Where is that folder with regards to your script ?
            Where are you calling your script from ?

            P Offline
            P Offline
            pablomichea
            wrote on 12 May 2022, 21:26 last edited by pablomichea 5 Dec 2022, 21:27
            #5

            @SGaist Hi, you can even download their folder from github and it doesn't work, here it is: https://github.com/Apress/Beginning-PyQt--second-edition

            chapter 2: labels.py

            1 Reply Last reply
            0
            • P pablomichea
              12 May 2022, 20:50

              Thanks you JonB, i understand what you mean, i just wanted to know why it won't work. I'm currently studying the book "Beginning PyQt" (2nd edition) and well.. they used a relative path but it doesn't work for me, it just work with an absolute path.

              Thanks!!

              J Offline
              J Offline
              JonB
              wrote on 12 May 2022, 22:22 last edited by
              #6

              @pablomichea said in Quick question about loading images:

              they used a relative path but it doesn't work for me, it just work with an absolute path.

              They were naughty! If you ensure your current directory is where that labels.py is located then images/... will be found relative to it. That must be what they intend.

              P 1 Reply Last reply 12 May 2022, 22:45
              0
              • J JonB
                12 May 2022, 22:22

                @pablomichea said in Quick question about loading images:

                they used a relative path but it doesn't work for me, it just work with an absolute path.

                They were naughty! If you ensure your current directory is where that labels.py is located then images/... will be found relative to it. That must be what they intend.

                P Offline
                P Offline
                pablomichea
                wrote on 12 May 2022, 22:45 last edited by
                #7

                @JonB yep that doesn't work, just uncompress the github folder and you'll notice it won't load the image.. unless my computer is just broken :P

                J 1 Reply Last reply 13 May 2022, 05:28
                0
                • P pablomichea
                  12 May 2022, 22:45

                  @JonB yep that doesn't work, just uncompress the github folder and you'll notice it won't load the image.. unless my computer is just broken :P

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 13 May 2022, 05:28 last edited by
                  #8

                  @pablomichea Do you know what a relative path is?
                  As @JonB told you a relative path is relative to the current working directory of your running application.
                  If the working directory is not the one where images subfolder is your path will not be found.
                  Either put your images into ressource files or construct absolute path at runtime.
                  You can also get the directory where you executable is: https://doc.qt.io/archives/qt-4.8/qcoreapplication.html#applicationDirPath, then add "images/world.png" to it...

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

                  1 Reply Last reply
                  3
                  • F Offline
                    F Offline
                    friedemannkleint
                    wrote on 20 May 2022, 10:01 last edited by
                    #9

                    You can also take a look at our QML examples.
                    They use pathlib to determine the path to the current source file:

                    Path(__file__).resolve().parent / 'view.qml')
                    
                    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