Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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"?
    Using:

    int calcHeight = fm.height();
    

    I get the same value.

    So can I get the multiline height?


  • Lifetime Qt Champion

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


Log in to reply