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. Icon on QPushButton does not show when running the application (Qt Widgets, CMake, .qrc)
Qt 6.11 is out! See what's new in the release blog

Icon on QPushButton does not show when running the application (Qt Widgets, CMake, .qrc)

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 1.3k 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.
  • C Offline
    C Offline
    CheeseLatte
    wrote on last edited by CheeseLatte
    #1

    Hello, everyone :)
    I created a very simple Qt Widgets project in Qt Creator. Then I created a res.qrc file and added a .ico image from the img folder into it.
    I also added the res.qrc file to my CMakeLists.txt. In Qt Creator's Design, I dragged a QPushButton onto the widget and set its icon using the image from the resource file.

    However, when I run the program, the button icon does not appear at all.

    What could be the reason for this?Did I miss something when adding the .qrc resource file or setting the icon?

    project dir

    -.qtcreator
    -build
    -img
    -- exit.png
    -CMakeLists.txt
    -dialog.cpp
    -dialog.h
    -dialog.ui
    -main.cpp
    -res.qrc

    CMakeLists.txt

    eb1c52c3-5b5a-4d0d-a8fe-508f55844a17-image.png

    res.qrc

    9eda5984-ac5a-4595-8c56-13ebc8970f61-image.png
    b448c06e-bb74-4b1e-ba0e-91a1d336014d-image.png

    dialog.ui

    9c5801d3-7fcc-4cf6-b852-5952173f9b79-image.png
    bf91e012-4acb-4893-b9e8-25ff9d2eb1dd-image.png

    dialog.cpp

    "qDebug() << icon.isNull();" always output true
    d21a01a2-59bf-4534-8a38-f5fed3593a12-image.png

    main.cpp

    a3058706-f73e-4fbd-b673-59e054a2e195-image.png

    JonBJ 1 Reply Last reply
    0
    • JonBJ JonB

      @CheeseLatte
      I think you have not added it correctly to CMakeLists.txt? For the .qrc I think you need qt_add_resources()?

      I don't know, but do you need to do this editing of the CMakeLists.txt manually? I don't use resources, but I assume Creator would sort out the CMakeLists.txt for you if you add a resource from inside it?

      C Offline
      C Offline
      CheeseLatte
      wrote on last edited by
      #3

      @JonB Thanks!
      I think I’ve found the cause of the problem.
      It turned out that RCC was not executed automatically, so the icons were not included in the build.
      After adding

      set(CMAKE_AUTORCC ON)
      

      to my CMakeLists.txt and rebuilding the project, the icons started showing correctly.

      Thanks again for your help :)

      JonBJ 1 Reply Last reply
      1
      • C CheeseLatte

        Hello, everyone :)
        I created a very simple Qt Widgets project in Qt Creator. Then I created a res.qrc file and added a .ico image from the img folder into it.
        I also added the res.qrc file to my CMakeLists.txt. In Qt Creator's Design, I dragged a QPushButton onto the widget and set its icon using the image from the resource file.

        However, when I run the program, the button icon does not appear at all.

        What could be the reason for this?Did I miss something when adding the .qrc resource file or setting the icon?

        project dir

        -.qtcreator
        -build
        -img
        -- exit.png
        -CMakeLists.txt
        -dialog.cpp
        -dialog.h
        -dialog.ui
        -main.cpp
        -res.qrc

        CMakeLists.txt

        eb1c52c3-5b5a-4d0d-a8fe-508f55844a17-image.png

        res.qrc

        9eda5984-ac5a-4595-8c56-13ebc8970f61-image.png
        b448c06e-bb74-4b1e-ba0e-91a1d336014d-image.png

        dialog.ui

        9c5801d3-7fcc-4cf6-b852-5952173f9b79-image.png
        bf91e012-4acb-4893-b9e8-25ff9d2eb1dd-image.png

        dialog.cpp

        "qDebug() << icon.isNull();" always output true
        d21a01a2-59bf-4534-8a38-f5fed3593a12-image.png

        main.cpp

        a3058706-f73e-4fbd-b673-59e054a2e195-image.png

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

        @CheeseLatte
        I think you have not added it correctly to CMakeLists.txt? For the .qrc I think you need qt_add_resources()?

        I don't know, but do you need to do this editing of the CMakeLists.txt manually? I don't use resources, but I assume Creator would sort out the CMakeLists.txt for you if you add a resource from inside it?

        C 1 Reply Last reply
        0
        • JonBJ JonB

          @CheeseLatte
          I think you have not added it correctly to CMakeLists.txt? For the .qrc I think you need qt_add_resources()?

          I don't know, but do you need to do this editing of the CMakeLists.txt manually? I don't use resources, but I assume Creator would sort out the CMakeLists.txt for you if you add a resource from inside it?

          C Offline
          C Offline
          CheeseLatte
          wrote on last edited by
          #3

          @JonB Thanks!
          I think I’ve found the cause of the problem.
          It turned out that RCC was not executed automatically, so the icons were not included in the build.
          After adding

          set(CMAKE_AUTORCC ON)
          

          to my CMakeLists.txt and rebuilding the project, the icons started showing correctly.

          Thanks again for your help :)

          JonBJ 1 Reply Last reply
          1
          • C CheeseLatte has marked this topic as solved on
          • C CheeseLatte

            @JonB Thanks!
            I think I’ve found the cause of the problem.
            It turned out that RCC was not executed automatically, so the icons were not included in the build.
            After adding

            set(CMAKE_AUTORCC ON)
            

            to my CMakeLists.txt and rebuilding the project, the icons started showing correctly.

            Thanks again for your help :)

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

            @CheeseLatte
            https://forum.qt.io/topic/143377/resources-management-in-qt-creator
            Like the poster there, I assumed Creator would manage adding this sort of thing for cmake when you put resources into a project. Looks like it worked without manual intervention with qmake.

            C 1 Reply Last reply
            0
            • JonBJ JonB

              @CheeseLatte
              https://forum.qt.io/topic/143377/resources-management-in-qt-creator
              Like the poster there, I assumed Creator would manage adding this sort of thing for cmake when you put resources into a project. Looks like it worked without manual intervention with qmake.

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

              @JonB
              That's right ! I haven't used qmake. CMake does require set(CMAKE_AUTORCC ON) to work, but there's a strange thing: the last time I created a project, I used CMake, and it loaded resources correctly even without "set(CMAKE_AUTORCC ON)". However, subsequent projects don't work that way.

              JonBJ 1 Reply Last reply
              0
              • C CheeseLatte

                @JonB
                That's right ! I haven't used qmake. CMake does require set(CMAKE_AUTORCC ON) to work, but there's a strange thing: the last time I created a project, I used CMake, and it loaded resources correctly even without "set(CMAKE_AUTORCC ON)". However, subsequent projects don't work that way.

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

                @CheeseLatte
                https://cmake.org/cmake/help/latest/prop_tgt/AUTORCC.html#prop_tgt:AUTORCC

                AUTORCC is a boolean specifying whether CMake will handle the Qt rcc code generator automatically, i.e. without having to use commands like qt4_add_resources(), qt5_add_resources(), etc. Currently, Qt versions 4 to 6 are supported.

                So did your other case have an explicit qt_add_resources() like I wrote earlier? It looks like you can use one or the other. I am thinking Creator should perhaps insert an qt_add_resources() when you add/create a resource file via its interface?

                C 1 Reply Last reply
                0
                • JonBJ JonB

                  @CheeseLatte
                  https://cmake.org/cmake/help/latest/prop_tgt/AUTORCC.html#prop_tgt:AUTORCC

                  AUTORCC is a boolean specifying whether CMake will handle the Qt rcc code generator automatically, i.e. without having to use commands like qt4_add_resources(), qt5_add_resources(), etc. Currently, Qt versions 4 to 6 are supported.

                  So did your other case have an explicit qt_add_resources() like I wrote earlier? It looks like you can use one or the other. I am thinking Creator should perhaps insert an qt_add_resources() when you add/create a resource file via its interface?

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

                  @JonB
                  In another case, I didn’t use set(CMAKE_AUTORCC ON) or qt_add_resources(), and it still worked correctly.
                  When using qt_add_executable(), it is not necessary to call qt_add_resources() explicitly — you can simply list the .qrc file directly in qt_add_executable().

                  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