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 6.7k 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.
  • J JonB
    11 May 2023, 09:20

    @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 11 May 2023, 09:57 last edited by lukutis222 5 Nov 2023, 10:02
    #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

    S J 2 Replies Last reply 11 May 2023, 11:14
    0
    • L lukutis222
      11 May 2023, 09:57

      @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

      S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 11 May 2023, 11:14 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 11 May 2023, 11:18
      0
      • L lukutis222
        11 May 2023, 09:57

        @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

        J Offline
        J Offline
        JonB
        wrote on 11 May 2023, 11:17 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
        • S SGaist
          11 May 2023, 11:14

          @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 11 May 2023, 11:18 last edited by lukutis222 5 Nov 2023, 11:18
          #15

          @SGaist
          Okay. Thanks for clarifying :)

          1 Reply Last reply
          0
          • S SGaist
            11 May 2023, 11:14

            @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 11 May 2023, 11:18 last edited by lukutis222 5 Nov 2023, 11:19
            #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 11 May 2023, 12:49
            0
            • J JonB
              11 May 2023, 08:45

              @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 11 May 2023, 11:46 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?

              J 1 Reply Last reply 11 May 2023, 11:54
              0
              • C CPPUIX
                11 May 2023, 11:46

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

                J Offline
                J Offline
                JonB
                wrote on 11 May 2023, 11:54 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 11 May 2023, 12:08
                1
                • J JonB
                  11 May 2023, 11:54

                  @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 11 May 2023, 12:08 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!

                  J 1 Reply Last reply 11 May 2023, 12:30
                  1
                  • C CPPUIX
                    11 May 2023, 12:08

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

                    J Offline
                    J Offline
                    JonB
                    wrote on 11 May 2023, 12:30 last edited by JonB 5 Nov 2023, 12:44
                    #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
                      11 May 2023, 11:18

                      @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 11 May 2023, 12:49 last edited by CPPUIX 5 Nov 2023, 12:52
                      #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 12 May 2023, 04:48
                      1
                      • C CPPUIX
                        11 May 2023, 12:49

                        @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 12 May 2023, 04:48 last edited by lukutis222 5 Dec 2023, 04:58
                        #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 12 May 2023, 07:19
                        0
                        • L lukutis222
                          12 May 2023, 04:48

                          @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 12 May 2023, 07:19 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

                          21/23

                          11 May 2023, 12:49

                          • Login

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