Results 1 to 3 of 3

Thread: Scale centered text

  1. #1
    Join Date
    Jun 2011
    Posts
    25
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Scale centered text

    Hi all, I want to use the scale property of a Text so that the enclosed text shows up in its entirety (no wrap) within the size of the Text itself.
    I’d like to be able to operate directly on the scale property instead of changing the font size.

    Everything works, as long as the text is left-aligned. If I switch the text alignment to center, the text box appears in the right place, but the text is not center-aligned.
    I tried to change the transformOrigin to “Item.Center”, but this does not solve the problem.

    Attached you can find an example featuring a text and a rectangle enclosing it:
    Qt Code:
    1. Rectangle
    2. {
    3. id: rect
    4. anchors.fill: testText
    5. color: "yellow"
    6. }
    7.  
    8. Text
    9. {
    10. id: testText
    11.  
    12. width: 100
    13. height: 50
    14.  
    15. anchors.centerIn: parent
    16.  
    17. text: "example"
    18. verticalAlignment: Text.AlignVCenter
    19. horizontalAlignment: Text.AlignHCenter
    20.  
    21. scale: Math.min((width / paintedWidth), (height / paintedHeight))
    22. transformOrigin: Item.Center // <- I've tried every point...
    23. color: "black"
    24. }
    To copy to clipboard, switch view to plain text mode 

    here you can see the (wrong) result: http://i59.tinypic.com/29c9ymg.jpg

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Scale centered text

    Have you tried without the alignment properties? The anchor setting should already take care of the centering.

    Cheers,
    _

  3. #3
    Join Date
    Jun 2011
    Posts
    25
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Scale centered text

    Thank you for the answer.
    I tried but it does not seem to work well.
    Without text alignments: http://i60.tinypic.com/24mgtn4.png
    With only vertical center alignment: http://i59.tinypic.com/25f6hro.jpg


    Added after 32 minutes:


    And in any case I don't know why but:
    Qt Code:
    1. scale: Math.min((width / paintedWidth), (height / paintedHeight))
    To copy to clipboard, switch view to plain text mode 
    does not seem to work perfectly!

    I tried with a vertical expansion changing the text to "aa". In this case, (width / paintedWidth) is greather than (height / paintedHeight), therefore the scale should be (height / paintedHeight) and I expect the text height will be the same as the box. But the result is: http://i62.tinypic.com/2vvq0xs.png

    What is wrong in scale computation?


    Added after 27 minutes:


    I found a workaround adding a wrapper Item and keeping Text auto-resizing:
    Qt Code:
    1. Rectangle
    2. {
    3. id: rect
    4. anchors.fill: textContainer
    5. color: "yellow"
    6. }
    7.  
    8. Item
    9. {
    10. id: textContainer
    11.  
    12. width: 100
    13. height: 50
    14.  
    15. anchors.centerIn: parent
    16. }
    17.  
    18. Text
    19. {
    20. id: testText
    21.  
    22. // NOTE: auto width
    23.  
    24. anchors.centerIn: textContainer
    25.  
    26. text: "aa"
    27. //verticalAlignment: Text.AlignTop // <-- NOT NEEDED
    28. //horizontalAlignment: Text.AlignLeft // <-- NOT NEEDED
    29.  
    30. scale: Math.min((textContainer.width / paintedWidth), (textContainer.height / paintedHeight))
    31. //transformOrigin: Item.Center // <-- NOT NEEDED
    32. color: "black"
    33. }
    To copy to clipboard, switch view to plain text mode 

    This seems to work but vertical expansion still does not fill the whole height.
    Please correct me if I'm wrong, but I believe paintedHeight should be the height in pixel of the text being rendered.
    However, this does not seem the case, as a border of a few pixels still remains, both in the upper and the lower part of the rectangle.
    Is this behavior due to the render engine leaving a few extra pixels for characters potentially higher than 'a' (e.g., 't' or 'q'), since the font being used is not monospace?
    Thanks in advance.


    Added after 21 minutes:


    The answer is… NO
    http://oi58.tinypic.com/2qu3xio.jpg

    Is this a QML bug?
    Last edited by nick85; 4th August 2014 at 12:17.

Similar Threads

  1. drawPoint not centered
    By Alundra in forum Qt Programming
    Replies: 1
    Last Post: 3rd November 2013, 07:38
  2. Scale Image centered
    By metRo_ in forum Qt Programming
    Replies: 2
    Last Post: 23rd August 2010, 20:25
  3. Bar Chart centered on Labels
    By liversedge in forum Qwt
    Replies: 1
    Last Post: 30th January 2010, 19:57
  4. Why QFileDialog::getOpenFileName is not centered?
    By ricardo in forum Qt Programming
    Replies: 20
    Last Post: 30th August 2009, 23:08
  5. centered text label
    By djconnel in forum Qwt
    Replies: 5
    Last Post: 28th April 2009, 17:22

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.