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. Can't center text in the Rectangle
Qt 6.11 is out! See what's new in the release blog

Can't center text in the Rectangle

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 802 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.
  • I Offline
    I Offline
    iliya faramarzi
    wrote on last edited by
    #1

    Hi,
    I have a Rectangle with a text inside of that and i want to make it centrize at the Rectangle. this is my Code:

    RowLayout{
        Layout.fillWidth: true
        Rectangle{
            width: 35
            height: 35
            radius: 35
            color: "#D9D9D9"
            Text {
                text: qsTr("+")
                font.family: regularFont.name
                font.pixelSize: 25
                fontSizeMode: Text.Fit
                wrapMode: Text.WordWrap
                anchors.fill: parent
                verticalAlignment: Text.AlignVCenter
                horizontalAlignment: Text.AlignHCenter
            }
        }
    }
    

    and this is how it looks like:
    MyAppProblem.png

    how can i make it centrize?

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

      The text ("+") is central to the Rectangle (grey circle) from what I can see.

      Otherwise, you need to create another Rectangle, make it black and place the original Rectangle (grey circle) inside the black Rectangle. For now, I've omitted RowLayout;

      // black rectangle
      Rectangle {
          width: 135
          height: 235
          color: "black"
      
          // grey circle with "+" in the center
          Rectangle{
              width: 35
              height: width
              radius: width/2
              color: "#D9D9D9"
              anchors.centerIn: parent
      
              // text "+"
              Text {
                  text: qsTr("+")
                  font.pixelSize: 25
                  anchors.centerIn: parent
              }
          }
      }
      

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

      I live by the sea, not in it.

      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