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
Forum Updated to NodeBB v4.3 + New Features

Quick question about loading images

Scheduled Pinned Locked Moved Unsolved Qt for Python
9 Posts 5 Posters 650 Views 1 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.
  • pablomicheaP Offline
    pablomicheaP Offline
    pablomichea
    wrote on 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!!

    JonBJ 1 Reply Last reply
    0
    • pablomicheaP pablomichea

      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!!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #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
      • pablomicheaP Offline
        pablomicheaP Offline
        pablomichea
        wrote on 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!!

        JonBJ 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on 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

          pablomicheaP 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

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

            pablomicheaP Offline
            pablomicheaP Offline
            pablomichea
            wrote on last edited by pablomichea
            #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
            • pablomicheaP pablomichea

              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!!

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on 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.

              pablomicheaP 1 Reply Last reply
              0
              • JonBJ JonB

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

                pablomicheaP Offline
                pablomicheaP Offline
                pablomichea
                wrote on 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

                jsulmJ 1 Reply Last reply
                0
                • pablomicheaP pablomichea

                  @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

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 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 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