Results 1 to 6 of 6

Thread: Center text on painter

  1. #1
    Join Date
    Apr 2006
    Location
    the netherlands
    Posts
    17
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Center text on painter

    Hi All,

    How can I center text at a given x position on a painter?

    I use a function to draw lines and above the line I want to set the value of the line (like 0, 2, 4, 6 etc) How can I center these values? I now use something like p.drawText(x,y,`bla') but there must be a way to find out how large 'bla' will be in pixels?

    Thanks in advance
    Rob

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Center text on painter

    Use QFontMetrics to find the height, width, ascent and descent of the text you use.

  3. The following user says thank you to munna for this useful post:

    masoroso (2nd May 2006)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Center text on painter

    Quote Originally Posted by masoroso
    I now use something like p.drawText(x,y,`bla') but there must be a way to find out how large 'bla' will be in pixels?
    Either use QPainter::boundingRect() or paint the text using different QPainter::drawText() method (the one which takes QRect as the first parameter).

  5. The following user says thank you to jacek for this useful post:

    masoroso (2nd May 2006)

  6. #4
    Join Date
    Apr 2006
    Location
    the netherlands
    Posts
    17
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Center text on painter

    thanks!

    I now use

    Qt Code:
    1. QFontMetrics fm(painter.font());
    2. ...
    3. tw = fm.width(sx);
    4. painter.drawText(x+tw/2, y, sx);
    To copy to clipboard, switch view to plain text mode 

    btw. I did not understand the boundingRect option... I still need to know the rect dimensions before I call `boundingrect'??

    Anyway, it works this way so thank you both,
    Rob

  7. #5
    Join Date
    Apr 2006
    Location
    the netherlands
    Posts
    17
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Center text on painter

    whoops sorry..

    boundingrect is a better option in this case because QFontMetrics does not have a height() option (and I also need to align to the center of the height).

    The code is now rewritten to

    Qt Code:
    1. QFontMetrics fm(painter.font());
    2. ..
    3. xoffset = fm.boundingRect(sx).width()/2;
    4. painter.drawText(x - xoffset, y, sx);
    To copy to clipboard, switch view to plain text mode 
    Last edited by masoroso; 2nd May 2006 at 14:33.

  8. The following user says thank you to masoroso for this useful post:

    FireMan (22nd May 2008)

  9. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Center text on painter

    Quote Originally Posted by masoroso
    I still need to know the rect dimensions before I call `boundingrect'??
    No, you don't have to:
    If the text does not fit within the given rectangle using the specified flags, the function returns the required rectangle.

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Replies: 1
    Last Post: 21st November 2009, 20:38
  3. Problem with Painter in Qt4.4.1
    By merry in forum Qt Programming
    Replies: 1
    Last Post: 1st September 2008, 13:47
  4. Match the text beetween two string
    By dreamer in forum Qt Programming
    Replies: 4
    Last Post: 20th May 2008, 14:48
  5. Editable text in QGraphicsView
    By wysota in forum Qt Programming
    Replies: 8
    Last Post: 24th February 2007, 15:30

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.