Results 1 to 7 of 7

Thread: Height of the text without balnk space at the top

  1. #1
    Join Date
    Mar 2007
    Location
    Ukraine, Odessa
    Posts
    140
    Thanks
    15
    Thanked 11 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Height of the text without balnk space at the top

    Hello everyone
    I need to center text in a rectangle and I use QFontMetrics::height() to know the height of it.
    But it doesn't return me the height but instead it gives me blank space at the top
    Qt Code:
    1. QPainter painter(this);
    2. painter.setPen(QPen(QColor(Qt::black)));
    3. QFont font = QFont("Times", 50, QFont::Normal);
    4. painter.setFont(font);
    5. QFontMetrics metrics(font);
    6.  
    7. int heightOfText = metrics.height();
    8. painter.drawLine(0, 200 - heightOfText,
    9. 1000, 200 - heightOfText);
    10. painter.drawLine(0, 200, 1000, 200);
    11. painter.drawText(0, 200, "Gradient Text");
    To copy to clipboard, switch view to plain text mode 
    This code gives me the result displayed on the screenshot below
    So My question is how to find text height without that blank space ?
    (I suppose it has something with ascent and descent...)
    Attached Images Attached Images
    • File Type: jpg 1.jpg (8.1 KB, 8 views)
    Last edited by THRESHE; 3rd March 2008 at 14:04.
    C++ & AMD forever

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Height of the text without balnk space at the top

    or something with lineSpacing() ?? am not sure...

    by the way in which case u need to draw lines for text ?? just curious abt what u implemented

  3. #3
    Join Date
    Mar 2007
    Location
    Ukraine, Odessa
    Posts
    140
    Thanks
    15
    Thanked 11 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Height of the text without balnk space at the top

    Lines are just for debugging And I've tried line spacing but it didn't help
    C++ & AMD forever

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Height of the text without balnk space at the top

    Just for debugging ??
    will this string wont help in this case ??
    str = "-------------------------------------------------------------";

    why do u need such complex code that lines shud be just touching text ??
    wont printing the above simple string solve the purpose ??

  5. #5
    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: Height of the text without balnk space at the top


  6. #6
    Join Date
    Mar 2007
    Location
    Ukraine, Odessa
    Posts
    140
    Thanks
    15
    Thanked 11 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Height of the text without balnk space at the top

    Tried that already it didn't work.

    Closest to the height of the text is
    Qt Code:
    1. QFont font = QFont("Trebushet MS", 50, QFont::Normal);
    2. QFontMetrics metrics(font);
    3.  
    4. int height = metrics.ascent() - metrics.descent() + metrics.leading();
    To copy to clipboard, switch view to plain text mode 
    C++ & AMD forever

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Height of the text without balnk space at the top

    Hi,

    the space on top is what is needed for letters like "gjqy" under the baseline. By drawing the Text
    Qt Code:
    1. painter.drawText(0, 200, "Gradient Text");
    To copy to clipboard, switch view to plain text mode 
    you set the baseline at 200. To get the Text vertical centered use
    Qt Code:
    1. painter.drawText(0, 200+(metrics.boundingRect("m").y()/2), "Gradient Text");
    To copy to clipboard, switch view to plain text mode 
    if you have no "gy..." or use
    Qt Code:
    1. void QPainter::drawText ( int x, int y, int width, int height, int flags, const QString & text, QRect * boundingRect = 0 )
    To copy to clipboard, switch view to plain text mode 
    with Qt::AlignHCenter.


    Lykurg

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. linking user space and kernel space programs with qmake
    By zielchri in forum Qt Programming
    Replies: 9
    Last Post: 8th March 2006, 23:11

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.