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

Load icon from resource

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 5 Posters 17.2k Views 3 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.
  • Leo ZL Offline
    Leo ZL Offline
    Leo Z
    wrote on last edited by
    #1

    Hello everyone, I meet a problem which is quite weird.
    I set up a treeview and add some rows in it. Then I want to add icons for them. It goes well when I add them by

    //myicon = new QIcon("C:/Users/yz001/Documents/Qt/Learning Qt/QtToDoListV1_0/inbox.png");
    

    However, when I add the picture into my resource and add them by

    myicon = new QIcon(":/inbox.png");
    

    They just don't show up.
    Could you please me how to make it via resource?
    The following is part of my code:

        ui->treeView->setModel(taskModel);  //set model
        ui->treeView->header()->hide();     //hide header
        ui->treeView->setRootIsDecorated(false);    //hide the root
    
        //myicon = new QIcon("C:/Users/yz001/Documents/Qt/Learning Qt/QtToDoListV1_0/inbox.png");
        myicon = new QIcon(":/icons/inbox.png");
        
        QStandardItem* item_inbox = new QStandardItem(QString("Inbox"));
        QStandardItem* item_starred = new QStandardItem(QString("Starred"));
        QStandardItem* item_today = new QStandardItem(QString("Today"));
        QStandardItem* item_week = new QStandardItem(QString("Week"));
    
        item_inbox->setIcon(*myicon);
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      You must add a qres file to you project
      Right click this and add picture to it.

      Then you can use
      new QIcon(":/inbox.png"); syntax
      ":/" is syntax for images/files in a res file.

      http://www.bogotobogo.com/Qt/Qt5_Resource_Files.php
      use the first pat on how to add the qres file.
      Stop at
      "Finish. Then, the Designer will provide us with resource editor."
      Then right click this new file (Myres) and add image to it.

      1 Reply Last reply
      1
      • Leo ZL Offline
        Leo ZL Offline
        Leo Z
        wrote on last edited by Leo Z
        #3

        @mrjj
        I think I did add a qres file like this.
        Is this enough to use the resource?

        mrjjM 1 Reply Last reply
        1
        • Leo ZL Leo Z

          @mrjj
          I think I did add a qres file like this.
          Is this enough to use the resource?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @Leo-Z
          it looks fine.
          Right click the actual image and u can get right path directly. (Copy path)
          It will be like ":/icons/inbox.png"

          Leo ZL 1 Reply Last reply
          1
          • mrjjM mrjj

            @Leo-Z
            it looks fine.
            Right click the actual image and u can get right path directly. (Copy path)
            It will be like ":/icons/inbox.png"

            Leo ZL Offline
            Leo ZL Offline
            Leo Z
            wrote on last edited by
            #5

            @mrjj
            I did that too actually. It's kind of frustrating. Code and application

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              The path seems fine. Make sure you re-run qmake (Build->run qmake) after you change your resources.

              On another note you've created a memory leak. Don't create QIcons on the heap, there's no point. This is enough:

              QStandardItem* item_inbox = new QStandardItem(QIcon(":/icons/inbox.png"), "Inbox");
              
              Leo ZL 1 Reply Last reply
              2
              • Chris KawaC Chris Kawa

                The path seems fine. Make sure you re-run qmake (Build->run qmake) after you change your resources.

                On another note you've created a memory leak. Don't create QIcons on the heap, there's no point. This is enough:

                QStandardItem* item_inbox = new QStandardItem(QIcon(":/icons/inbox.png"), "Inbox");
                
                Leo ZL Offline
                Leo ZL Offline
                Leo Z
                wrote on last edited by
                #7

                @Chris-Kawa
                Thank you. It works after run qmake. But could you please tell me why I need to run qmake after change my resources?In fact, this is my first time to run qmake before build a app. I still don't know the role of the qmake.

                1 Reply Last reply
                0
                • Chris KawaC Offline
                  Chris KawaC Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by Chris Kawa
                  #8

                  qmake is the tool that generates makefiles that your build tool uses to compile the project. It also runs moc, uic, ad rcc tools to generate QObject metadata, transform .ui into .h files and transform the .qrc file into .cpp file (open the build directory to see the generated files). In most cases it runs automatically (like when you add a file to your project or edit a .ui file), but it's spotty in some situations and you need to run it manually. A good rule of thumb is to run it manually whenever you change contents of the .pro file or modify resources. In general a majority of "weird behavior" like program not seeing some changes you made in code/config can be fixed be re-running qmake.

                  Leo ZL 1 Reply Last reply
                  2
                  • Chris KawaC Chris Kawa

                    qmake is the tool that generates makefiles that your build tool uses to compile the project. It also runs moc, uic, ad rcc tools to generate QObject metadata, transform .ui into .h files and transform the .qrc file into .cpp file (open the build directory to see the generated files). In most cases it runs automatically (like when you add a file to your project or edit a .ui file), but it's spotty in some situations and you need to run it manually. A good rule of thumb is to run it manually whenever you change contents of the .pro file or modify resources. In general a majority of "weird behavior" like program not seeing some changes you made in code/config can be fixed be re-running qmake.

                    Leo ZL Offline
                    Leo ZL Offline
                    Leo Z
                    wrote on last edited by
                    #9

                    @Chris-Kawa
                    Thank you. This information is really helpful for me:)

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      michals
                      wrote on last edited by
                      #10

                      And how do you debug when it does not work?

                      I built a project that appears to follow these steps but does not have icon.

                      JonBJ 1 Reply Last reply
                      0
                      • M michals

                        And how do you debug when it does not work?

                        I built a project that appears to follow these steps but does not have icon.

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

                        @michals said in Load icon from resource:

                        And how do you debug when it does not work?

                        • Check QFile::exists(":/icons/inbox.png")?
                        • Check QIcon(":/icons/inbox.png").isNull()?
                        • Check what QImage(":/icons/inbox.png") actually looks like?
                        1 Reply Last reply
                        2
                        • M Offline
                          M Offline
                          michals
                          wrote on last edited by
                          #12

                          Thanks for your help

                          QFile::exists(":/install/x11-transparency-tools.svg") 1
                          QIcon(":/install/x11-transparency-tools.svg").isNull() 0

                          I can load the icon fine when I specify a file path but for some reason it was failing when using the resource. Now I returned to loading from resource and it works with the same code that was there initially. Not sure what the problem was. And that's exactly the frustrating part - the documentation is not very clear on this resource loading and when it breaks you have no idea why.

                          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