Measuring text width in Qt

前端 未结 4 1424
别跟我提以往
别跟我提以往 2020-12-15 02:51

Using the Qt framework, how do I measure the width (in pixels) of a piece of text rendered with a given font/style?

4条回答
  •  死守一世寂寞
    2020-12-15 03:03

    Since Qt 5.11 you must use horizontalAdvance() method of QFontMetrics class instead of width(). width() is now obselete.

    QFont myFont(fontName, fontSize);;
    QString str("I wonder how wide this is?");
    
    QFontMetrics fm(myFont);
    int width=fm.horizontalAdvance(str);
    

提交回复
热议问题