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. [SOLVED] Horizontally centering a rectangle in a window

[SOLVED] Horizontally centering a rectangle in a window

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 1.8k 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.
  • B Offline
    B Offline
    Binary91
    wrote on last edited by
    #1

    Hi,

    I'm trying to horizontally center a rectangle in a window. This is my code:
    @Window
    {
    visible: true
    width: 400
    height: 600

    Rectangle
    {
    id: rectLayout
    anchors.fill: parent

    Rectangle
    {
      width: 90
      height: 25
      anchors.top: rectLayout.top
      anchors.left: rectLayout.left
      anchorrs.topMargin: 5
      anchors.horizontalCenter: rectLayout.horizontalCenter
    }
    

    }
    }@
    The wanted effect is a rectangle with top margin to the window of 5px and horizontally centered with a total width of 90 and height of 25 pixels.

    The problem is, that using anchors.horizontalCenter: rectLayout.horizontalCenter results in streching the rectangle to the whole width of the window...

    Does anybody know how I can avoid this effect?

    Thank you in anticipation :-)

    1 Reply Last reply
    0
    • GianlucaG Offline
      GianlucaG Offline
      Gianluca
      wrote on last edited by
      #2

      Remove anchors.left: rectLayout.left
      What did you do is to anchor the rectangle to the left and the horizontalCenter to the center, so Qt in order to maintain both stretch the rectangle.

      1 Reply Last reply
      0
      • B Offline
        B Offline
        Binary91
        wrote on last edited by
        #3

        Oh yes, you're right. Thank you really much! :-)

        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