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. Button text alignment in ToolBar
Forum Updated to NodeBB v4.3 + New Features

Button text alignment in ToolBar

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 3 Posters 336 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.
  • C Offline
    C Offline
    Captain Haddock
    wrote on last edited by
    #1

    Just getting to grips with QML. Simple example constructed from samples I found. When you run it please see that he text in the button is not centralized. Guessing my example is fubar .. how should it be modified to centralize the text in the buttons please?

    main.qml:

    import QtQuick
    import QtQuick.Controls
    import QtQuick.Layouts
    
    ApplicationWindow {
        width: 1920
        height: 1200
        visible: true
        flags: Qt.FramelessWindowHint | Qt.Window
        title: qsTr("IGS")
        color: "#4C535D"
    
        ToolBar {
            ColumnLayout {
                Item {
                    Layout.preferredHeight: 10
                }
                MyButton {
                    text: qsTr("Auto\nID")
                }
                MyButton {
                    text: qsTr("?")
                    onClicked: Qt.exit(0)
                }
            }
        }
    
        StackView {
            id: stack
            anchors.fill: parent
        }
    }
    

    MyButton.qml:

    import QtQuick
    
    import QtQuick.Controls.Basic
    
    ItemDelegate {
        id: root
    
        property string state: "Selectable"
    
        contentItem: Text {
            text: root.text
            font.family: "Arial"
            font.pixelSize: 12
            color: "black"
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
        }
        background: Rectangle {
            width: 60
            implicitHeight: 60
            border {
                width: 4
                color: "black"
            }
            radius: 16
            color: root.state == "Selectable" ? "#979EA8" : root.state == "NotSelectable" ? "#4C535D" : "#84BECC"
        }
    }
    
    
    mzimmersM JoeCFDJ 2 Replies Last reply
    0
    • C Captain Haddock

      Just getting to grips with QML. Simple example constructed from samples I found. When you run it please see that he text in the button is not centralized. Guessing my example is fubar .. how should it be modified to centralize the text in the buttons please?

      main.qml:

      import QtQuick
      import QtQuick.Controls
      import QtQuick.Layouts
      
      ApplicationWindow {
          width: 1920
          height: 1200
          visible: true
          flags: Qt.FramelessWindowHint | Qt.Window
          title: qsTr("IGS")
          color: "#4C535D"
      
          ToolBar {
              ColumnLayout {
                  Item {
                      Layout.preferredHeight: 10
                  }
                  MyButton {
                      text: qsTr("Auto\nID")
                  }
                  MyButton {
                      text: qsTr("?")
                      onClicked: Qt.exit(0)
                  }
              }
          }
      
          StackView {
              id: stack
              anchors.fill: parent
          }
      }
      

      MyButton.qml:

      import QtQuick
      
      import QtQuick.Controls.Basic
      
      ItemDelegate {
          id: root
      
          property string state: "Selectable"
      
          contentItem: Text {
              text: root.text
              font.family: "Arial"
              font.pixelSize: 12
              color: "black"
              horizontalAlignment: Text.AlignHCenter
              verticalAlignment: Text.AlignVCenter
          }
          background: Rectangle {
              width: 60
              implicitHeight: 60
              border {
                  width: 4
                  color: "black"
              }
              radius: 16
              color: root.state == "Selectable" ? "#979EA8" : root.state == "NotSelectable" ? "#4C535D" : "#84BECC"
          }
      }
      
      
      mzimmersM Offline
      mzimmersM Offline
      mzimmers
      wrote on last edited by
      #2

      @Captain-Haddock your MyButton component is being used inside a layout, so it's best to use the Layout attached properties to size it, rather than sizing the background:

      ItemDelegate {
          id: root
          Layout.preferredHeight: 60
          Layout.preferredWidth: 60
      

      and you can remove the sizing from your background.

      BTW: is there a reason you're using ItemDelegate for this? A simple Pane should work just fine...

      1 Reply Last reply
      1
      • C Captain Haddock

        Just getting to grips with QML. Simple example constructed from samples I found. When you run it please see that he text in the button is not centralized. Guessing my example is fubar .. how should it be modified to centralize the text in the buttons please?

        main.qml:

        import QtQuick
        import QtQuick.Controls
        import QtQuick.Layouts
        
        ApplicationWindow {
            width: 1920
            height: 1200
            visible: true
            flags: Qt.FramelessWindowHint | Qt.Window
            title: qsTr("IGS")
            color: "#4C535D"
        
            ToolBar {
                ColumnLayout {
                    Item {
                        Layout.preferredHeight: 10
                    }
                    MyButton {
                        text: qsTr("Auto\nID")
                    }
                    MyButton {
                        text: qsTr("?")
                        onClicked: Qt.exit(0)
                    }
                }
            }
        
            StackView {
                id: stack
                anchors.fill: parent
            }
        }
        

        MyButton.qml:

        import QtQuick
        
        import QtQuick.Controls.Basic
        
        ItemDelegate {
            id: root
        
            property string state: "Selectable"
        
            contentItem: Text {
                text: root.text
                font.family: "Arial"
                font.pixelSize: 12
                color: "black"
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignVCenter
            }
            background: Rectangle {
                width: 60
                implicitHeight: 60
                border {
                    width: 4
                    color: "black"
                }
                radius: 16
                color: root.state == "Selectable" ? "#979EA8" : root.state == "NotSelectable" ? "#4C535D" : "#84BECC"
            }
        }
        
        
        JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by JoeCFD
        #3

        @Captain-Haddock Do not you set width and height to ItemDelegate?
        then make contentItem and background fill the parent?

        Why do you set
        width: 60
        implicitHeight: 60
        to background?

        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