Important: Please read the Qt Code of Conduct -

How to get font height from QTextEdit where text is multiline?

  • Hello,

        QTextEdit text("test");
        QFontMetrics fm(text.font());
        QString myText = text.toPlainText();
        int calcWidth = fm.horizontalAdvance(myText);
        int calcHeight = fm.height();

    When I use the code above I get width and height of text "test". But what if the text will be like "test\ntest2\ntest3"?

    int calcHeight = fm.height();

    I get the same value.

    So can I get the multiline height?

  • Lifetime Qt Champion

    You have to tell it to wordWrap and give it an area
    fm.boundingRect(QRect(0,0,width,100), Qt::TextWordWrap, text);
    Then you should be able to use newlines.

Log in to reply