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

Using relative path for image:url in stylesheet

Scheduled Pinned Locked Moved Unsolved General and Desktop
23 Posts 4 Posters 7.5k Views 2 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.
  • 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 Online
      JonBJ Online
      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 Online
          JonBJ Online
          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 Online
                JonBJ Online
                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 Online
                        JonBJ Online
                        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 Online
                            JonBJ Online
                            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
                            • L lukutis222

                              @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 Offline
                              C Offline
                              CPPUIX
                              wrote on last edited by CPPUIX
                              #21

                              @lukutis222 Could you share green_circle.png file? and the style sheet you're applying to your label?

                              I tested with a random green circle icon with a transparent background, and a yellow background for the label, and it works.

                              Here's how it looks:

                              greenyellow.png

                              and here's the image I used, and its link:

                              green.png

                              L 1 Reply Last reply
                              1
                              • C CPPUIX

                                @lukutis222 Could you share green_circle.png file? and the style sheet you're applying to your label?

                                I tested with a random green circle icon with a transparent background, and a yellow background for the label, and it works.

                                Here's how it looks:

                                greenyellow.png

                                and here's the image I used, and its link:

                                green.png

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

                                @Abderrahmene_Rayene
                                Sure. The image is attached:
                                green_circle.png

                                You should be able save it as image.

                                Regarrding the label:
                                I have simply created a label, adjusted its size and added stylesheet background color:
                                ded6475c-86ff-4a39-bf89-25c89585f0be-image.png

                                af0a1a92-db2d-4b67-8c35-243a7ca9e69a-image.png

                                In the code I do:

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

                                Result

                                20b8adac-59d1-4b54-95cc-28942c50e225-image.png

                                I have then tried the same thing again just instead using my green_circle, I used your green_circle. That seems to work fine!
                                9b4780e0-2afc-4f95-8fb0-01eb65b74100-image.png .

                                I wonder what is exactly t=the problem with my image. It seems transparent but its not!

                                It is also worth mentioning that this is not just one image that I have issues with. I use many transparent images that does not actually seem to be transparent. Another example is this icon:
                                ping.png

                                C 1 Reply Last reply
                                0
                                • L lukutis222

                                  @Abderrahmene_Rayene
                                  Sure. The image is attached:
                                  green_circle.png

                                  You should be able save it as image.

                                  Regarrding the label:
                                  I have simply created a label, adjusted its size and added stylesheet background color:
                                  ded6475c-86ff-4a39-bf89-25c89585f0be-image.png

                                  af0a1a92-db2d-4b67-8c35-243a7ca9e69a-image.png

                                  In the code I do:

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

                                  Result

                                  20b8adac-59d1-4b54-95cc-28942c50e225-image.png

                                  I have then tried the same thing again just instead using my green_circle, I used your green_circle. That seems to work fine!
                                  9b4780e0-2afc-4f95-8fb0-01eb65b74100-image.png .

                                  I wonder what is exactly t=the problem with my image. It seems transparent but its not!

                                  It is also worth mentioning that this is not just one image that I have issues with. I use many transparent images that does not actually seem to be transparent. Another example is this icon:
                                  ping.png

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

                                  @lukutis222 it seems this has nothing to do with Qt.

                                  How are you downloading these images? What's their source?

                                  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