Results 1 to 3 of 3

Thread: Horizontally centering a rectangle in another rectangle

  1. #1
    Join Date
    Nov 2014
    Location
    Germany
    Posts
    69
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Horizontally centering a rectangle in another rectangle

    Hi,

    I’m trying to horizontally center a rectangle in a window. This is my code:
    Qt Code:
    1. Window
    2. {
    3. visible: true
    4. width: 400
    5. height: 600
    6.  
    7. Rectangle
    8. {
    9. id: rectLayout
    10. anchors.fill: parent
    11.  
    12. Rectangle
    13. {
    14. width: 90
    15. height: 25
    16. anchors.top: rectLayout.top
    17. anchors.left: rectLayout.left
    18. anchorrs.topMargin: 5
    19. anchors.horizontalCenter: rectLayout.horizontalCenter
    20. }
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 
    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 :-)

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Horizontally centering a rectangle in another rectangle

    Your anchor definitions are contradictory. If you want to horizontally center an item, you cannot set its left anchor.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Nov 2014
    Location
    Germany
    Posts
    69
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Re: Horizontally centering a rectangle in another rectangle

    Hi,
    oh yes that was the problem! Thank you! :-)

Similar Threads

  1. Getting the bounding rectangle
    By ioannis in forum Qt Programming
    Replies: 1
    Last Post: 22nd May 2009, 01:41
  2. Drawing a selection rectangle
    By Caius Aérobus in forum Qt Programming
    Replies: 7
    Last Post: 4th April 2009, 16:47
  3. QFrame inner rectangle
    By Micawber in forum Qt Programming
    Replies: 6
    Last Post: 1st April 2009, 21:40
  4. How to get a round rectangle (QRegion)
    By quzcFroal in forum Qt Programming
    Replies: 7
    Last Post: 2nd June 2008, 22:14
  5. Display rectangle on Image
    By parmar ranjit in forum Qt Programming
    Replies: 3
    Last Post: 22nd February 2008, 14:09

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.