Results 1 to 3 of 3

Thread: Find the text width in QFont

  1. #1
    Join Date
    Feb 2014
    Posts
    60
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Find the text width in QFont

    I have two items in my tree model i have small difference in text alignment.Is this caused by the width of the text but i checked the width of text using QFontMetrics::width() but both text are same.

    Text1:111601756
    Text2:999999996

    Capture.PNG

    As from the image you can see there is a slight alignment problem in the second text.

    Here is the sample code i tried :-

    Qt Code:
    1. QFont font("times",24);
    2. QFontMetrics metrics(font);
    3. qDebug() << "Width 1" << metrics.width(QString::number(111111111));
    4. qDebug() << "Width 2" << metrics.width(QString::number(999999999));
    To copy to clipboard, switch view to plain text mode 

    Output:

    Width 1 153

    Width 2 153

    I expected Width 2 to be more what is going wrong here.Is the method i am checking is wrong.

  2. #2
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Find the text width in QFont

    Try to print "11111111" and "99999999" using times 24pt and see. Certain proportional fonts use fixed width for numbers so that they can align numeric values. Maybe, times uses the fixed width, too. The font from your picture does not align: "1" is clearly narrower than "9".

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

    anbu01 (18th August 2016)

  4. #3
    Join Date
    Feb 2014
    Posts
    60
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Find the text width in QFont

    Thanks it helped i changed the font.
    Last edited by anbu01; 18th August 2016 at 09:35.

Similar Threads

  1. QToolButton doesn't fit to text width
    By nnzz in forum Qt Programming
    Replies: 3
    Last Post: 13th July 2016, 12:21
  2. find the character width in the console
    By sajis997 in forum Newbie
    Replies: 1
    Last Post: 21st July 2015, 13:43
  3. Text width greater than the QRect
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 31st March 2011, 08:27
  4. Fastest way to get width of rich text
    By jgrauman in forum Qt Programming
    Replies: 2
    Last Post: 22nd June 2010, 22:07
  5. Replies: 3
    Last Post: 3rd May 2009, 08:58

Tags for this Thread

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.