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. Using relative path for image:url in stylesheet
Forum Update on Monday, May 27th 2025

Using relative path for image:url in stylesheet

Scheduled Pinned Locked Moved Unsolved General and Desktop
23 Posts 4 Posters 6.5k 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.
  • L Offline
    L Offline
    lukutis222
    wrote on last edited by lukutis222
    #1

    Hello. I would like to know how can I use relative paths in image:url in my stylesheet. I can only get it to work when I use absolute paths.

    For example, I have a checkbox with custom icon depending if it is checked or unchecked. Currently, I do that by adding the following to its stylesheet:

     QCheckBox::indicator {
         width: 18px;
         height: 18px;
     }
    
      QCheckBox::indicator:checked
      {
       image: url(C:/Users/petrikas.lu/Desktop/WORK/QT/PTL/simpleclient/icons/green_circle.png);
      }
      QCheckBox::indicator:unchecked
      {
        image: url(C:/Users/petrikas.lu/Desktop/WORK/QT/PTL/simpleclient/icons/red_circle.png);
      }
    
    

    In my project folder, I have icons folder where I keep all the icons. How can I correctly use the path instead of always showing the absolute paths?
    1eaccbc4-6b45-4d7c-81cc-7b8d0b961d5c-image.png

    Does it depend on what the working directory is set to?

    73a91862-5f74-4a2e-ade1-ffcf1710eb27-image.png

    Thanks in advance

    SGaistS 1 Reply Last reply
    0
    • L lukutis222

      Hello. I would like to know how can I use relative paths in image:url in my stylesheet. I can only get it to work when I use absolute paths.

      For example, I have a checkbox with custom icon depending if it is checked or unchecked. Currently, I do that by adding the following to its stylesheet:

       QCheckBox::indicator {
           width: 18px;
           height: 18px;
       }
      
        QCheckBox::indicator:checked
        {
         image: url(C:/Users/petrikas.lu/Desktop/WORK/QT/PTL/simpleclient/icons/green_circle.png);
        }
        QCheckBox::indicator:unchecked
        {
          image: url(C:/Users/petrikas.lu/Desktop/WORK/QT/PTL/simpleclient/icons/red_circle.png);
        }
      
      

      In my project folder, I have icons folder where I keep all the icons. How can I correctly use the path instead of always showing the absolute paths?
      1eaccbc4-6b45-4d7c-81cc-7b8d0b961d5c-image.png

      Does it depend on what the working directory is set to?

      73a91862-5f74-4a2e-ade1-ffcf1710eb27-image.png

      Thanks in advance

      SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      If you want to simplify things, you should consider using the Qt resources system so the icons are embedded in your executable and not dependent on the binary location.

      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
      4
      • C Offline
        C Offline
        CPPUIX
        wrote on last edited by CPPUIX
        #3

        Hi,

        You should implement @SGaist's suggestion, it's way more efficient.

        But if you insist on your current approach, you just need to realize that the path is relative to your program's executable location, which you can find in your build folder (build-simpleclient-Desktop...), not your source folder (simpleclient).

        So you could either copy your icons into your build folder.

        Or you could use the icons where they currently are without changing there location like this:

        image: url(../icons/green_circle.png);

        ../ means parent folder.

        For more clarification, here's the equivalent to your relative paths:

        image: url(C:/Users/petrikas.lu/Desktop/WORK/QT/PTL/simpleclient/build-simpleclient-Desktop_Qt.../icons/green_circle.png);

        Which is wrong because such file does not exist, hence why it doesn't work.

        Again, I advice you to implement @SGaist's suggestion.

        Hope this helps!

        JonBJ 1 Reply Last reply
        0
        • C CPPUIX

          Hi,

          You should implement @SGaist's suggestion, it's way more efficient.

          But if you insist on your current approach, you just need to realize that the path is relative to your program's executable location, which you can find in your build folder (build-simpleclient-Desktop...), not your source folder (simpleclient).

          So you could either copy your icons into your build folder.

          Or you could use the icons where they currently are without changing there location like this:

          image: url(../icons/green_circle.png);

          ../ means parent folder.

          For more clarification, here's the equivalent to your relative paths:

          image: url(C:/Users/petrikas.lu/Desktop/WORK/QT/PTL/simpleclient/build-simpleclient-Desktop_Qt.../icons/green_circle.png);

          Which is wrong because such file does not exist, hence why it doesn't work.

          Again, I advice you to implement @SGaist's suggestion.

          Hope this helps!

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Abderrahmene_Rayene said in Using relative path for image:url in stylesheet:

          Or you could use the icons where they currently are without changing there location like this:
          image: url(../icons/green_circle.png);
          ../ means parent folder.

          "Parent folder" from where is it interpreted as?? If it's parent from runtime working directory it's quite useless....

          C 1 Reply Last reply
          0
          • JonBJ JonB

            @Abderrahmene_Rayene said in Using relative path for image:url in stylesheet:

            Or you could use the icons where they currently are without changing there location like this:
            image: url(../icons/green_circle.png);
            ../ means parent folder.

            "Parent folder" from where is it interpreted as?? If it's parent from runtime working directory it's quite useless....

            C Offline
            C Offline
            CPPUIX
            wrote on last edited by
            #5

            @JonB sorry, I don't understand, could you rephrase?

            JonBJ 1 Reply Last reply
            0
            • C CPPUIX

              @JonB sorry, I don't understand, could you rephrase?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @Abderrahmene_Rayene
              Not sure how to phrase it any clearer. You talk about using a relative path for url(...) argument, yours starts with ../. Which does indeed mean "parent directory". But I am asking you: parent directory from where?? Parent of which directory?

              Do you mean parent from "the current working directory at runtime of the program"? If so, since you have no idea what that might be when running the program I don't see how it helps or how I could ever use it for production. It might be useful if you are developing in Creator and cause that to set your runtime working directory to somewhere known when spawning your executable, but outside of that I cannot see that it helps. If it meant, say, "relative to the directory in which the executable is located" then it would be useful, but I don't think Qt treats it that way.

              C 1 Reply Last reply
              0
              • JonBJ JonB

                @Abderrahmene_Rayene
                Not sure how to phrase it any clearer. You talk about using a relative path for url(...) argument, yours starts with ../. Which does indeed mean "parent directory". But I am asking you: parent directory from where?? Parent of which directory?

                Do you mean parent from "the current working directory at runtime of the program"? If so, since you have no idea what that might be when running the program I don't see how it helps or how I could ever use it for production. It might be useful if you are developing in Creator and cause that to set your runtime working directory to somewhere known when spawning your executable, but outside of that I cannot see that it helps. If it meant, say, "relative to the directory in which the executable is located" then it would be useful, but I don't think Qt treats it that way.

                C Offline
                C Offline
                CPPUIX
                wrote on last edited by
                #7

                @JonB said in Using relative path for image:url in stylesheet:

                relative to the directory in which the executable is located

                Yes that is what I meant, my post is based on OP's current folder hierarchy, their build folder is in their working directory.

                I know this is not production level, but OP seems to not understand how this works, like I was just 2 months ago, so I'm just providing an explanation based on their current situation, not a general one.

                The screenshot they shared shows their current hierarchy which I based my explanation to OP's specific situation on. I just thought they shouldn't move on from this without actually understanding what they did wrong.

                L JonBJ 2 Replies Last reply
                0
                • C CPPUIX

                  @JonB said in Using relative path for image:url in stylesheet:

                  relative to the directory in which the executable is located

                  Yes that is what I meant, my post is based on OP's current folder hierarchy, their build folder is in their working directory.

                  I know this is not production level, but OP seems to not understand how this works, like I was just 2 months ago, so I'm just providing an explanation based on their current situation, not a general one.

                  The screenshot they shared shows their current hierarchy which I based my explanation to OP's specific situation on. I just thought they shouldn't move on from this without actually understanding what they did wrong.

                  L Offline
                  L Offline
                  lukutis222
                  wrote on last edited by lukutis222
                  #8

                  Thanks for the help. I have ended up using the QT resource system as you guys suggsted. I refer to this documentation:
                  https://doc.qt.io/qt-5/resources.html

                  and found this Youtube video to be very easy to follow and got it to work quite fast.
                  https://www.youtube.com/watch?v=b2urUjl03EY&ab_channel=MacDigia

                  1 Reply Last reply
                  0
                  • C CPPUIX

                    @JonB said in Using relative path for image:url in stylesheet:

                    relative to the directory in which the executable is located

                    Yes that is what I meant, my post is based on OP's current folder hierarchy, their build folder is in their working directory.

                    I know this is not production level, but OP seems to not understand how this works, like I was just 2 months ago, so I'm just providing an explanation based on their current situation, not a general one.

                    The screenshot they shared shows their current hierarchy which I based my explanation to OP's specific situation on. I just thought they shouldn't move on from this without actually understanding what they did wrong.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @Abderrahmene_Rayene
                    I understand you are trying to help the OP, and my comment is not intended to "criticise" you. But you say

                    you just need to realize that the path is relative to your program's executable location

                    Although I have not tested, I think this incorrect. Unless you show otherwise or have a reference for that for a Qt url(), I see no evidence it takes the location of the executable into account at all. Rather I believe it is relative to the *runtime current directory", which could be anywhere at all, not related to the executable's location. Which means using a relative path is pretty useless to locate any existing file to read, and consequently we recommend people not to use a relative path. When building and running from Creator, that may set the program's current directory to the "build" folder where the executable is located, but that is just a Creator thing, and won't apply outside. Hence @SGaist's suggestion of using Qt's resources, whose paths always work.

                    If the user tries from a terminal:

                    cd /tmp
                    /path/to/build/folder/executable-file
                    

                    does it then find the original ../icons/green_circle.png, or does it say it does not exist because it looks for /icons/green_circle.png?

                    L C 2 Replies Last reply
                    0
                    • JonBJ JonB

                      @Abderrahmene_Rayene
                      I understand you are trying to help the OP, and my comment is not intended to "criticise" you. But you say

                      you just need to realize that the path is relative to your program's executable location

                      Although I have not tested, I think this incorrect. Unless you show otherwise or have a reference for that for a Qt url(), I see no evidence it takes the location of the executable into account at all. Rather I believe it is relative to the *runtime current directory", which could be anywhere at all, not related to the executable's location. Which means using a relative path is pretty useless to locate any existing file to read, and consequently we recommend people not to use a relative path. When building and running from Creator, that may set the program's current directory to the "build" folder where the executable is located, but that is just a Creator thing, and won't apply outside. Hence @SGaist's suggestion of using Qt's resources, whose paths always work.

                      If the user tries from a terminal:

                      cd /tmp
                      /path/to/build/folder/executable-file
                      

                      does it then find the original ../icons/green_circle.png, or does it say it does not exist because it looks for /icons/green_circle.png?

                      L Offline
                      L Offline
                      lukutis222
                      wrote on last edited by lukutis222
                      #10

                      @Abderrahmene_Rayene
                      @JonB
                      @SGaist

                      This is a little out of topic but its relevant.

                      What is the best way to display an icon without any additional widgets.
                      I have noticed that not all widgets support setIcon method.
                      bbbc1b37-0be5-4a2c-aca5-d354ce023176-image.png

                      For example , for my particular application, I now have the following:

                      void MainWindow::on_pushButton_clicked()
                      {
                          if(ui->status_checkbox_test->checkState() == Qt::Unchecked){
                              ui->status_checkbox_test->setCheckState(Qt::Checked);
                              ui->status_checkbox_test->setIcon(QIcon(":/icons/green_circle.png"));
                      
                              return;
                          }
                          else if(ui->status_checkbox_test->checkState() == Qt::Checked){
                              ui->status_checkbox_test->setCheckState(Qt::Unchecked);
                              ui->status_checkbox_test->setIcon(QIcon(":/icons/red_circle.png"));
                      
                              return;
                          }
                      }
                      

                      Checkbox toggled:
                      8b671a5c-6ad6-410e-aab0-2e792606fcea-image.png

                      Checkbox not toggled:
                      77540016-2dee-4961-a6cd-a19249e0804d-image.png

                      My issue

                      Since I am using QCheckBox, even after setting the icon using setIcon I am seeing the checkbox text and the actual checkbox. However, in my case I would only like to display icon without anything else. Is there a widget in QT that is dedicated just for icon?

                      Simply said:

                      What if I want to simply insert an icon somewhere on the screen. What widget would I choose since I need a widget to use setIcon property?

                      JonBJ 1 Reply Last reply
                      0
                      • L lukutis222

                        @Abderrahmene_Rayene
                        @JonB
                        @SGaist

                        This is a little out of topic but its relevant.

                        What is the best way to display an icon without any additional widgets.
                        I have noticed that not all widgets support setIcon method.
                        bbbc1b37-0be5-4a2c-aca5-d354ce023176-image.png

                        For example , for my particular application, I now have the following:

                        void MainWindow::on_pushButton_clicked()
                        {
                            if(ui->status_checkbox_test->checkState() == Qt::Unchecked){
                                ui->status_checkbox_test->setCheckState(Qt::Checked);
                                ui->status_checkbox_test->setIcon(QIcon(":/icons/green_circle.png"));
                        
                                return;
                            }
                            else if(ui->status_checkbox_test->checkState() == Qt::Checked){
                                ui->status_checkbox_test->setCheckState(Qt::Unchecked);
                                ui->status_checkbox_test->setIcon(QIcon(":/icons/red_circle.png"));
                        
                                return;
                            }
                        }
                        

                        Checkbox toggled:
                        8b671a5c-6ad6-410e-aab0-2e792606fcea-image.png

                        Checkbox not toggled:
                        77540016-2dee-4961-a6cd-a19249e0804d-image.png

                        My issue

                        Since I am using QCheckBox, even after setting the icon using setIcon I am seeing the checkbox text and the actual checkbox. However, in my case I would only like to display icon without anything else. Is there a widget in QT that is dedicated just for icon?

                        Simply said:

                        What if I want to simply insert an icon somewhere on the screen. What widget would I choose since I need a widget to use setIcon property?

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #11

                        @lukutis222 said in Using relative path for image:url in stylesheet:

                        What if I want to simply insert an icon somewhere on the screen. What widget would I choose since I need a widget to use setIcon property?

                        QLabel has setPixmap(const QPixmap &), which accepts more than setIcon() would, but can take icons too.

                        L 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @lukutis222 said in Using relative path for image:url in stylesheet:

                          What if I want to simply insert an icon somewhere on the screen. What widget would I choose since I need a widget to use setIcon property?

                          QLabel has setPixmap(const QPixmap &), which accepts more than setIcon() would, but can take icons too.

                          L Offline
                          L Offline
                          lukutis222
                          wrote on last edited by lukutis222
                          #12

                          @JonB
                          Sure, I can use QPixmap to dispaly an icon:
                          b8c50e31-4ffa-4f49-8749-d880ff56ded5-image.png

                          This method uses QLabel to replace with icon:

                              QPixmap* pixmap =new QPixmap (":/icons/green_circle.png");
                              ui->test_label->setPixmap(*pixmap);
                              ui->test_label->setScaledContents(true);
                          

                          Is this a common way to do this in QT? If I just want to display an Icon, I would create a label widget and just replace it icon?

                          Additionally, My image does not actually seem to have transparent background even though when I open it using image editor it shows as transparent.

                          I have set QLabel background color to test it out:
                          851a6686-7243-4128-9b5d-972a68fb7ee4-image.png

                          The result:
                          857bfd87-91b1-4521-aaf4-3709792abaad-image.png

                          I would have expected to see green circle in yellow background

                          SGaistS JonBJ 2 Replies Last reply
                          0
                          • L lukutis222

                            @JonB
                            Sure, I can use QPixmap to dispaly an icon:
                            b8c50e31-4ffa-4f49-8749-d880ff56ded5-image.png

                            This method uses QLabel to replace with icon:

                                QPixmap* pixmap =new QPixmap (":/icons/green_circle.png");
                                ui->test_label->setPixmap(*pixmap);
                                ui->test_label->setScaledContents(true);
                            

                            Is this a common way to do this in QT? If I just want to display an Icon, I would create a label widget and just replace it icon?

                            Additionally, My image does not actually seem to have transparent background even though when I open it using image editor it shows as transparent.

                            I have set QLabel background color to test it out:
                            851a6686-7243-4128-9b5d-972a68fb7ee4-image.png

                            The result:
                            857bfd87-91b1-4521-aaf4-3709792abaad-image.png

                            I would have expected to see green circle in yellow background

                            SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @lukutis222 said in Using relative path for image:url in stylesheet:

                            QPixmap* pixmap =new QPixmap (":/icons/green_circle.png");
                            ui->test_label->setPixmap(*pixmap);
                            ui->test_label->setScaledContents(true);

                            Don't create the QPixmap on the heap, there's no need for that.

                            As for your other question, QLabel is a class made to show text or images so yes, it's pretty common to use it to show images like that. You could also implement your own subclass that paints the image but there would not be much benefit from that.

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

                            L 2 Replies Last reply
                            0
                            • L lukutis222

                              @JonB
                              Sure, I can use QPixmap to dispaly an icon:
                              b8c50e31-4ffa-4f49-8749-d880ff56ded5-image.png

                              This method uses QLabel to replace with icon:

                                  QPixmap* pixmap =new QPixmap (":/icons/green_circle.png");
                                  ui->test_label->setPixmap(*pixmap);
                                  ui->test_label->setScaledContents(true);
                              

                              Is this a common way to do this in QT? If I just want to display an Icon, I would create a label widget and just replace it icon?

                              Additionally, My image does not actually seem to have transparent background even though when I open it using image editor it shows as transparent.

                              I have set QLabel background color to test it out:
                              851a6686-7243-4128-9b5d-972a68fb7ee4-image.png

                              The result:
                              857bfd87-91b1-4521-aaf4-3709792abaad-image.png

                              I would have expected to see green circle in yellow background

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by
                              #14

                              @lukutis222 said in Using relative path for image:url in stylesheet:

                              I would have expected to see green circle in yellow background

                              Does your green_circle.png have transparent background?

                              1 Reply Last reply
                              0
                              • SGaistS SGaist

                                @lukutis222 said in Using relative path for image:url in stylesheet:

                                QPixmap* pixmap =new QPixmap (":/icons/green_circle.png");
                                ui->test_label->setPixmap(*pixmap);
                                ui->test_label->setScaledContents(true);

                                Don't create the QPixmap on the heap, there's no need for that.

                                As for your other question, QLabel is a class made to show text or images so yes, it's pretty common to use it to show images like that. You could also implement your own subclass that paints the image but there would not be much benefit from that.

                                L Offline
                                L Offline
                                lukutis222
                                wrote on last edited by lukutis222
                                #15

                                @SGaist
                                Okay. Thanks for clarifying :)

                                1 Reply Last reply
                                0
                                • SGaistS SGaist

                                  @lukutis222 said in Using relative path for image:url in stylesheet:

                                  QPixmap* pixmap =new QPixmap (":/icons/green_circle.png");
                                  ui->test_label->setPixmap(*pixmap);
                                  ui->test_label->setScaledContents(true);

                                  Don't create the QPixmap on the heap, there's no need for that.

                                  As for your other question, QLabel is a class made to show text or images so yes, it's pretty common to use it to show images like that. You could also implement your own subclass that paints the image but there would not be much benefit from that.

                                  L Offline
                                  L Offline
                                  lukutis222
                                  wrote on last edited by lukutis222
                                  #16

                                  @SGaist I am pretty sure it does. But it does not seem transparent when I use it as icon in QT.
                                  657c8c26-0d41-4d75-bf80-9fba3e947948-image.png

                                  I was expecting it to inherit whatever the background the QLabel is set to.

                                  C 1 Reply Last reply
                                  0
                                  • JonBJ JonB

                                    @Abderrahmene_Rayene
                                    I understand you are trying to help the OP, and my comment is not intended to "criticise" you. But you say

                                    you just need to realize that the path is relative to your program's executable location

                                    Although I have not tested, I think this incorrect. Unless you show otherwise or have a reference for that for a Qt url(), I see no evidence it takes the location of the executable into account at all. Rather I believe it is relative to the *runtime current directory", which could be anywhere at all, not related to the executable's location. Which means using a relative path is pretty useless to locate any existing file to read, and consequently we recommend people not to use a relative path. When building and running from Creator, that may set the program's current directory to the "build" folder where the executable is located, but that is just a Creator thing, and won't apply outside. Hence @SGaist's suggestion of using Qt's resources, whose paths always work.

                                    If the user tries from a terminal:

                                    cd /tmp
                                    /path/to/build/folder/executable-file
                                    

                                    does it then find the original ../icons/green_circle.png, or does it say it does not exist because it looks for /icons/green_circle.png?

                                    C Offline
                                    C Offline
                                    CPPUIX
                                    wrote on last edited by
                                    #17

                                    @JonB said in Using relative path for image:url in stylesheet:

                                    my comment is not intended to "criticise" you.

                                    I appreciate any feedback I can get, thank you!

                                    I can see your point, which made me doubt my past experiments and understandings, as I'm still at the beginning of my learning journey.

                                    So I made a little experiment. I created a new project with a check box, used OP's styelsheet, with my suggested relative path:

                                    image: url(../icons/green_circle.png);

                                    I then made a whole new directory separated from the project folder, and created a folder called executableFolder, and one called icons. When I run the executable (copied from the build directory), the stylesheet works.

                                    Here are the directories:

                                    user@user:~/Desktop/UnrelatedDir$ ls
                                    executableFolder  icons
                                    user@user:~/Desktop/UnrelatedDir$ cd executableFolder/
                                    user@user:~/Desktop/UnrelatedDir/executableFolder$ ls
                                    relativePath
                                    user@user:~/Desktop/UnrelatedDir/executableFolder$ cd ../
                                    user@user:~/Desktop/UnrelatedDir$ cd icons/
                                    user@user:~/Desktop/UnrelatedDir/icons$ ls
                                    green_circle.png  red_circle.png
                                    

                                    relativePath is the executable.

                                    Also, I made sure those images exist nowhere but in icons folder in the UnrelatedDir directory.

                                    Is this the correct experiment to demonstrate how relative paths work? If not, what should I change or try, in order to properly understand how it works? And is there something I should be aware of, that I seem to be ignorant about?

                                    JonBJ 1 Reply Last reply
                                    0
                                    • C CPPUIX

                                      @JonB said in Using relative path for image:url in stylesheet:

                                      my comment is not intended to "criticise" you.

                                      I appreciate any feedback I can get, thank you!

                                      I can see your point, which made me doubt my past experiments and understandings, as I'm still at the beginning of my learning journey.

                                      So I made a little experiment. I created a new project with a check box, used OP's styelsheet, with my suggested relative path:

                                      image: url(../icons/green_circle.png);

                                      I then made a whole new directory separated from the project folder, and created a folder called executableFolder, and one called icons. When I run the executable (copied from the build directory), the stylesheet works.

                                      Here are the directories:

                                      user@user:~/Desktop/UnrelatedDir$ ls
                                      executableFolder  icons
                                      user@user:~/Desktop/UnrelatedDir$ cd executableFolder/
                                      user@user:~/Desktop/UnrelatedDir/executableFolder$ ls
                                      relativePath
                                      user@user:~/Desktop/UnrelatedDir/executableFolder$ cd ../
                                      user@user:~/Desktop/UnrelatedDir$ cd icons/
                                      user@user:~/Desktop/UnrelatedDir/icons$ ls
                                      green_circle.png  red_circle.png
                                      

                                      relativePath is the executable.

                                      Also, I made sure those images exist nowhere but in icons folder in the UnrelatedDir directory.

                                      Is this the correct experiment to demonstrate how relative paths work? If not, what should I change or try, in order to properly understand how it works? And is there something I should be aware of, that I seem to be ignorant about?

                                      JonBJ Offline
                                      JonBJ Offline
                                      JonB
                                      wrote on last edited by
                                      #18

                                      @Abderrahmene_Rayene said in Using relative path for image:url in stylesheet:

                                      Is this the correct experiment to demonstrate how relative paths work?

                                      No! This demonstrates what you already have/know from Creator. Because of your cd executableFolder/ you are precisely making sure the current directory HAPPENS to be where the executable lives. This is also how Creator runs your app. So ../icons/green_circle.png is correct just for this case.

                                      But you don't know where the user might be when they run your app. All you have to do to test is what I wrote above:

                                      cd /tmp
                                      ~/Desktop/UnrelatedDir/executableFolder/relativePath
                                      

                                      Now does it still pick up the green_circle.png?

                                      C 1 Reply Last reply
                                      1
                                      • JonBJ JonB

                                        @Abderrahmene_Rayene said in Using relative path for image:url in stylesheet:

                                        Is this the correct experiment to demonstrate how relative paths work?

                                        No! This demonstrates what you already have/know from Creator. Because of your cd executableFolder/ you are precisely making sure the current directory HAPPENS to be where the executable lives. This is also how Creator runs your app. So ../icons/green_circle.png is correct just for this case.

                                        But you don't know where the user might be when they run your app. All you have to do to test is what I wrote above:

                                        cd /tmp
                                        ~/Desktop/UnrelatedDir/executableFolder/relativePath
                                        

                                        Now does it still pick up the green_circle.png?

                                        C Offline
                                        C Offline
                                        CPPUIX
                                        wrote on last edited by
                                        #19

                                        @JonB No it does not, and I get it now.

                                        In my case I made the executable's directory the working directory by navigating towards it and executing it by clicking on it in a file manager.

                                        To demonstrate that I understood, I copied the icons folder into tmp, and ran your command again, and it found green_circle.png.

                                        I think I understand now, Thank you!

                                        JonBJ 1 Reply Last reply
                                        1
                                        • C CPPUIX

                                          @JonB No it does not, and I get it now.

                                          In my case I made the executable's directory the working directory by navigating towards it and executing it by clicking on it in a file manager.

                                          To demonstrate that I understood, I copied the icons folder into tmp, and ran your command again, and it found green_circle.png.

                                          I think I understand now, Thank you!

                                          JonBJ Offline
                                          JonBJ Offline
                                          JonB
                                          wrote on last edited by JonB
                                          #20

                                          @Abderrahmene_Rayene said in Using relative path for image:url in stylesheet:

                                          by navigating towards it and executing it by clicking on it in a file manager

                                          Indeed, but that probably depends on your File Manager/OS. And after a while you/the user may set up a "shortcut" icon for the executable on the desktop and double-click that. Now what is the current directory when it is launched? Each OS/File Manager has its own settings or rules.

                                          So how can we locate supplied external files? One possibility that suits your case is to use QString QCoreApplication::applicationDirPath() to find where the executable is. If you installed the icons directory there or thereabouts you might append icons/ or ../icons/ to that path to locate the .png. That is OK from code, but you can't pass anything suitable to the original stylesheet's url(...) for this case. You might cd there from within your application's code, so that relative works like it does from Creator, but that has consequences, plus it can break QCoreApplication::applicationDirPath().... [Oh, and also it won't for Qt Python applications.]

                                          The upshot is, especially for this url(...) syntax, you are probably best off switching to Qt's resources to supply your icons instead of as external files, as @SGaist suggested and @lukutis222 has now adopted.

                                          P.S.
                                          What would really be better is if Qt "QSS" stylesheets either defined where url(...) sought relative paths somehow relative to where the executable is or some environment variable specifies, or added an extra syntax in url(...) to refer to the executable directory. If we were in HTML with CSS sheets, I believe relative paths are from where the page being viewed is located, and/or an absolute URL /... refers to the virtual root directory of the application, so one can work with that.

                                          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