Results 1 to 12 of 12

Thread: QFontMetrics, i can't understand

  1. #1
    Join Date
    May 2007
    Posts
    32
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default QFontMetrics, i can't understand

    QFont font;
    font.setBold(true);
    std::cout<<QFontMetrics(font).width("...");

    Writed 9.

    QFont font;
    std::cout<<QFontMetrics(font).width("...");

    Writed 12.

    Why if font is bold the width is smaller then if font is nobold?

  2. #2
    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: QFontMetrics, i can't understand

    It seems to work OK on my system. Which Qt version and what font do you use?

  3. #3
    Join Date
    May 2007
    Posts
    32
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QFontMetrics, i can't understand

    Qt 4.3

    I use only this code
    Qt Code:
    1. QFont font2;
    2. std::cout<<QFontMetrics(font2).width("...")<<"\n";
    3. font2.setBold(true);
    4. font2.setItalic(true);
    5. std::cout<<QFontMetrics(font2).width("...");
    To copy to clipboard, switch view to plain text mode 
    And it is write
    12
    9

  4. #4
    Join Date
    May 2007
    Posts
    32
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QFontMetrics, i can't understand

    if i don't use font2.setBold(true);
    it is write
    12
    12

  5. #5
    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: QFontMetrics, i can't understand

    I've just tried it on windows, and indeed I got 12 and 9, but if you start Qt Designer and compare two labels (one with normal font and second with bold font), you'll see it's a correct result for the default font.

  6. #6
    Join Date
    May 2007
    Posts
    32
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QFontMetrics, i can't understand

    it is a bug of the Qt?

  7. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFontMetrics, i can't understand

    Quote Originally Posted by jacek View Post
    I've just tried it on windows, and indeed I got 12 and 9, but if you start Qt Designer and compare two labels (one with normal font and second with bold font), you'll see it's a correct result for the default font.
    I can't confirm this with 4.3 on XP. A bold text has a bigger width than the same text, not bold.

    I'll try to see what QFontMetrics returns.

    Regards

  8. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFontMetrics, i can't understand

    Qt Code:
    1. fmetrics::fmetrics(QWidget *parent, Qt::WFlags flags)
    2. : QDialog(parent, flags)
    3. {
    4. ui.setupUi(this);
    5.  
    6. QFontMetrics metrics(ui.label->font());
    7. QFontMetrics metrics2(ui.label_2->font());
    8.  
    9. qDebug( QString("%1").arg(metrics.width(ui.label->text())).toAscii().constData() );
    10. qDebug( QString("%1").arg(metrics2.width(ui.label_2->text())).toAscii().constData() );
    11. }
    To copy to clipboard, switch view to plain text mode 

    label and label_2 are two QLabels with the same font set. One has normal font and the other has a bold font.

    I always get a smaller width for the label with normal font. I tried with several fonts.

    What font did you use?

    Regards

  9. #9
    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: QFontMetrics, i can't understand

    Quote Originally Posted by pakulo View Post
    it is a bug of the Qt?
    No, everything is OK. It's just a property of a font you have used.

    ss.png

  10. #10
    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: QFontMetrics, i can't understand

    Quote Originally Posted by marcel View Post
    I can't confirm this with 4.3 on XP.
    Try it on "..." and MS Shell Dlg 2 font.

  11. #11
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFontMetrics, i can't understand

    Yes, with "..." I get the same result.
    But with other font families this seems to be OK.

    Anyway, I don't see a real problem even with MS Shell Dlg 2, as long as the dimensions are correct( which they are ).

    So, there must be a glitch in the font.

    Regards

  12. #12
    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: QFontMetrics, i can't understand

    Quote Originally Posted by marcel View Post
    So, there must be a glitch in the font.
    There is no glitch. Simply MS Shell Dlg 2 behaves this way.

Similar Threads

  1. Stylesheet and QFontMetrics
    By ucomesdag in forum Qt Programming
    Replies: 7
    Last Post: 18th January 2010, 07:45
  2. QProgressBar: don't understand in details, help me !!!
    By haconganh in forum Qt Programming
    Replies: 5
    Last Post: 10th May 2007, 08:07

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.