Measuring text width in Qt

前端 未结 4 1420
别跟我提以往
别跟我提以往 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:22

    In the paintEvent

    QString text("text");
    QFontMetrics fm = painter.fontMetrics();
    int width = fm.width(text);
    

提交回复
热议问题