Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Toolbutton icon source
Forum Updated to NodeBB v4.3 + New Features

Toolbutton icon source

Scheduled Pinned Locked Moved Solved QML and Qt Quick
10 Posts 6 Posters 1.9k 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.
  • Q Offline
    Q Offline
    qcoderpro
    wrote on last edited by qcoderpro
    #1

    Hi all,

    In the following Qt 6 (CMake build) project:

    import QtQuick
    import QtQuick.Controls
    
    ApplicationWindow {
        id: window
        width: 600; height: 400
        visible: true
    
     /* Image {
            anchors.left: parent.left
            anchors.verticalCenter: parent.verticalCenter
            source: "images/menu.png"
        } */
        ToolButton {
            anchors.left: parent.left
            anchors.verticalCenter: parent.verticalCenter
            icon.source: "images/menu.png"
        }
    }
    

    the source of the image file works for the Image element, but the error below turns up when it's used for the source in ToolButton:
    qrc:/qt-project.org/imports/QtQuick/Controls/Material/ToolButton.qml: QML IconImage: Cannot open: qrc:/qt-project.org/imports/QtQuick/Controls/Material/images/menu.png

    How to solve it, please?

    1 Reply Last reply
    0
    • U Offline
      U Offline
      Ulysses Fonseca
      wrote on last edited by
      #9

      @qcoderpro

      For me solved using the Qt.resolvedUrl, like this:

      ToolButton {
              anchors.centerIn: parent
              icon.source: Qt.resolvedUrl("images/svg/settings.svg")
      }
      
      1 Reply Last reply
      1
      • Q Offline
        Q Offline
        qcoderpro
        wrote on last edited by
        #2

        No idea how to solve this issue!?

        1 Reply Last reply
        0
        • MarkkyboyM Offline
          MarkkyboyM Offline
          Markkyboy
          wrote on last edited by
          #3

          You may need to add the Url of the image/images into your .pro file;

          SOURCES += \
                  main.cpp
          
          RESOURCES += qml.qrc \
              images/previous.png \    <------
              images/next.png \    <-----
          

          By not adding this, i note that the project builds but the image/s cannot be opened;

          file:///C:/Qt/5.15.2/mingw81_32/qml/QtQuick/Controls.2/ToolButton.qml: QML IconImage: Cannot open: qrc:/images/next.png
          file:///C:/Qt/5.15.2/mingw81_32/qml/QtQuick/Controls.2/ToolButton.qml: QML IconImage: Cannot open: qrc:/images/previous.png
          

          Don't just sit there standing around, pick up a shovel and sweep up!

          I live by the sea, not in it.

          Q 1 Reply Last reply
          0
          • MarkkyboyM Markkyboy

            You may need to add the Url of the image/images into your .pro file;

            SOURCES += \
                    main.cpp
            
            RESOURCES += qml.qrc \
                images/previous.png \    <------
                images/next.png \    <-----
            

            By not adding this, i note that the project builds but the image/s cannot be opened;

            file:///C:/Qt/5.15.2/mingw81_32/qml/QtQuick/Controls.2/ToolButton.qml: QML IconImage: Cannot open: qrc:/images/next.png
            file:///C:/Qt/5.15.2/mingw81_32/qml/QtQuick/Controls.2/ToolButton.qml: QML IconImage: Cannot open: qrc:/images/previous.png
            
            Q Offline
            Q Offline
            qcoderpro
            wrote on last edited by
            #4

            @Markkyboy

            I'm suing CMake. As well as, the project doesn't include a .qrc file.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              arun-holoplot
              wrote on last edited by
              #5

              @qcoderpro

              You should use icon.name: "menu" instead of icon.source, then the icon will be loaded from the platform theme

              Ref:
              AbstractButton::icon
              QIconTheme

              Q 1 Reply Last reply
              0
              • A arun-holoplot

                @qcoderpro

                You should use icon.name: "menu" instead of icon.source, then the icon will be loaded from the platform theme

                Ref:
                AbstractButton::icon
                QIconTheme

                Q Offline
                Q Offline
                qcoderpro
                wrote on last edited by
                #6

                @arun-holoplot
                It shows the button but not the .png file:

                import QtQuick
                import QtQuick.Controls
                
                ApplicationWindow {
                    id: window
                    width: 600; height: 400
                    visible: true
                
                    header: ToolBar {
                           Flow {
                               anchors.fill: parent
                               ToolButton {
                                   text: qsTr("Open")
                                   icon.name: "menu"
                                  // icon.source:"images/menu.png"
                                   onClicked: console.log("Shown")
                               }
                           }
                       }
                }
                
                1 Reply Last reply
                0
                • U Offline
                  U Offline
                  Ulysses Fonseca
                  wrote on last edited by
                  #7

                  I'm with the same problem, i have a project porting from QT5.15.2 working, but now the images of ToolButton using icon.source, don't show, i add all images using qt_add_qml_module by RESOURCES adding each svg image, on the Image work well, but not on ToolButton.

                  1 Reply Last reply
                  0
                  • fcarneyF Offline
                    fcarneyF Offline
                    fcarney
                    wrote on last edited by
                    #8

                    I don't know if this will help, but I dug this up earlier:
                    https://forum.qt.io/topic/91984/adding-qrc-icon-resource-to-a-cmake-qt-project/14

                    Read the whole thing.

                    C++ is a perfectly valid school of magic.

                    1 Reply Last reply
                    0
                    • U Offline
                      U Offline
                      Ulysses Fonseca
                      wrote on last edited by
                      #9

                      @qcoderpro

                      For me solved using the Qt.resolvedUrl, like this:

                      ToolButton {
                              anchors.centerIn: parent
                              icon.source: Qt.resolvedUrl("images/svg/settings.svg")
                      }
                      
                      1 Reply Last reply
                      1
                      • S Offline
                        S Offline
                        StudentQT
                        wrote on last edited by
                        #10

                        Even though this thread is old and solved, I want to share the solution, you've probably been looking for:

                        import QtQuick.Controls.Basic
                        
                        Button {
                           contentItem: Row {
                                IconImage {
                                    source: mySource
                                    color: myColor
                                }
                                Text {
                                    ... 
                                }
                            }
                            background: Rectangle {
                                ...
                            }
                        }       
                        

                        Just stumbled over it by accident (QtCreator's autocorrection offered it to me). IconImage seems to be the private Item, which Qt uses for the icon property of the Abstractbutton or the Action Element (https://doc.qt.io/qt-6/qtquickcontrols-icons.html)
                        Thats why you have to import the QtQuick.Controls.Basic
                        Hope it helps someone, who has the same problem.

                        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