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. How to fit Button to icon size?
Forum Updated to NodeBB v4.3 + New Features

How to fit Button to icon size?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 3 Posters 186 Views 1 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.
  • K Offline
    K Offline
    Kobid
    wrote last edited by
    #1

    Hi,
    Is it possible to act Button control like ToolButton? I have set display: AbstractButton.IconOnly but it still has extra space. I have tried everything:

    Button {
    spacing: 0
    padding: 0
    leftInset: 0
    rightInset: 0
    leftPadding: 0
    Layout.margins: 0
    horizontalPadding: 0
    verticalPadding: 0
    display: AbstractButton.IconOnly
    icon.source: "qrc:/icons/icons/plus-solid-full.svg"
    icon.color: myPalette.buttonText
    }
    

    ToolButton works as expected (green rectangle) but it doesn't have background even if I set flat: false:
    39cee7ae-ece8-4e96-9837-23d1f93b4bbc-obraz.png

    1 Reply Last reply
    0
    • GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote last edited by
      #4

      Your issue is caused by the implicitWidth of the button being the max of its content's implicitWidth (+ padding) and its background's implicitWidth.
      One quick workaround is to to background.implicitWidht: 0 to ignore it and just use the one from the icon or change the implicitWidth of the button to depend only on the content:

      Button {
          display: AbstractButton.IconOnly
          icon.source: "qrc:/icons/icons/plus-solid-full.svg"
          icon.color: myPalette.buttonText
          implicitWidth: implicitContentWidth + leftPadding + rightPadding
      }
      

      In this case I would maybe instead customize a ToolButton with background.visible: true. I assume you are using the Fusion style.

      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote last edited by
        #2

        Hi,

        You can fix the size of the button in your code for example as you know the size of the icons.
        Another option could be to add a spacer at the end of the layout to push the button against the combo box.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • K Offline
          K Offline
          Kobid
          wrote last edited by
          #3

          I would preffer icon fit to button size than inversely. That is because controls have some preffered size due to its own role and also screen DPI and scale (I'm working on 4k display) so all controls nicely fit to each other's height in layouts. So in this case height should be default and width=icon.width. But with this solution icon is smaller:

          Button {
                                 display: AbstractButton.IconOnly
                                 icon.source: "qrc:/icons/icons/plus-solid-full.svg"
                                 icon.color: myPalette.buttonText
                                 // icon.height: 24
                                 // icon.width: 24
                                 implicitWidth: icon.width
                             }
          

          1fb0035a-f386-449c-9d76-c0e6b3143b27-obraz.png
          When I set some icon width, for example 24:

          Button {
                                  display: AbstractButton.IconOnly
                                  icon.source: "qrc:/icons/icons/plus-solid-full.svg"
                                  icon.color: myPalette.buttonText
                                  // icon.height: 24
                                  icon.width: 24
                                  implicitWidth: icon.width
                              }
          

          ffea5107-94ed-461d-8c27-01483d5ea5fc-obraz.png
          Then button's height become larger than other buttons. This is situation which I mentioned at the beginning - I don't know how to predict preffered icon size for user OS, theme and resolution.
          About spacer. Can't find equivalent for qtwidget's spacer so I added Item at the end:

          Item {
                                  Layout.fillWidth: true
                              }
          

          But result looks like on screens above - it doesn't squeeze buttons

          1 Reply Last reply
          0
          • GrecKoG Offline
            GrecKoG Offline
            GrecKo
            Qt Champions 2018
            wrote last edited by
            #4

            Your issue is caused by the implicitWidth of the button being the max of its content's implicitWidth (+ padding) and its background's implicitWidth.
            One quick workaround is to to background.implicitWidht: 0 to ignore it and just use the one from the icon or change the implicitWidth of the button to depend only on the content:

            Button {
                display: AbstractButton.IconOnly
                icon.source: "qrc:/icons/icons/plus-solid-full.svg"
                icon.color: myPalette.buttonText
                implicitWidth: implicitContentWidth + leftPadding + rightPadding
            }
            

            In this case I would maybe instead customize a ToolButton with background.visible: true. I assume you are using the Fusion style.

            1 Reply Last reply
            1
            • K Offline
              K Offline
              Kobid
              wrote last edited by Kobid
              #5

              @GrecKo Thank you! Indeed both of your tricks do the job, for Button and ToolButton. For background.visible: true I would never figure it out - I was convinced that flat: false should do that.

              8805d7b3-d2cf-42f3-bca5-fd3400ab8ee2-obraz.png

              1 Reply Last reply
              0
              • K Kobid has marked this topic as solved
              • GrecKoG Offline
                GrecKoG Offline
                GrecKo
                Qt Champions 2018
                wrote last edited by
                #6

                To figure that out I read the source code of fusion/ToolButton.qml

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote last edited by
                  #7

                  Damn ! I somehow missed that you were using QML 😅
                  Sorry for the wrong hints. I am glad that @GrecKo was able to help you

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  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