Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTextEdit resize on content change?



  • Hi, is it possible to set up a QTextEdit widget so it changes it's height depending on the amount of text inside the widget? I like it to just use enough vertical space to show the content and not expand to all available vertical space if that is not needed.

    And i'd like to have the parent frame grow or shrink depending of the child textedit frame.

    Is it possible?


  • Lifetime Qt Champion

    Hi,

    Is it going to be read-only text ?



  • @SGaist said in QTextEdit resize on content change?:

    Hi,

    Is it going to be read-only text ?

    Yep, i just write text to it from code.


  • Lifetime Qt Champion

    Then why not use QLabel ?



  • @SGaist said in QTextEdit resize on content change?:

    Then why not use QLabel ?

    Well i write text from a database into it and use html tables to format it. From the documentation it looked like this was a good choice and the inserthmtl convenience function is handy. Should i use a label instead?


  • Lifetime Qt Champion

    I would do some testing with the data you are using. QLabel supports a subset of HTML (see setTextFormat)



  • @SGaist said in QTextEdit resize on content change?:

    I would do some testing with the data you are using. QLabel supports a subset of HTML (see setTextFormat)

    I finally did some testing. The label does the resize like i want but it doesn't give the same html/css results. Also it doesn't do scrolling/scrollbar. The documentation says:

    Note that QLabel is well-suited to display small rich text documents, such as small documents that get their document specific settings (font, text color, link color) from the label's palette and font properties. For large documents, use QTextEdit in read-only mode instead. QTextEdit can also provide a scroll bar when necessary.
    

    So it's fine to use a QTextEdit or QTextBrowser. Which leaves me with the original question of how to get the auto resizing if that's even possible...


  • Lifetime Qt Champion

    @Tink
    Hi
    You can try with

    (in ctor )
      qDebug() << connect( ui->textEdit->document()->documentLayout(), SIGNAL( documentSizeChanged(const QSizeF&) ),  this, SLOT( DocRect(const QSizeF&) ) );
    ----
    
    void MainWindow::DocRect(const QSizeF& r) {
      ui->textEdit->setMaximumHeight(int((r.height())));
    }
    
    

    Seems to get height ok. ( +/- a few pixels that might just be rounding)
    alt text



  • @mrjj hey, this works very good!

    Now the only thing i need to fix is how to get all the other frames to play nice together. It seems this setup doesn't like the use of Layout Alignments. I have the QTextEdit in a parent frame with some other frames and if i don't use alignments and set the parent frame to use layoutSizeConstraint: setMinandMaxSize, it works. But any change to alignment like AlignTop and the resize fails. Perhaps you have some idea why this happens?

    Anyways i'm going to mark this as solved, thanks!



  • I noticed that sometimes a loop would occur between the documentSizeChanged and setMaximumHeight resulting in an unresponsive QTextEdit. So now i write to another QTextDocument first and then do a setDocument on the QTextEdit and a setMaximumHeight based on the temporary QTextDocument. Which seems to work most of the time.


  • Lifetime Qt Champion

    @Tink
    hi good to hear.
    Im not sure why alignment would not work with fixed Heights.
    Let me try when i get home.

    • Loop would occur between the documentSizeChanged and setMaximumHeight

    Yes, I did think about that for a moment but seemed not happen for me.
    but you seem to be able to have that effect.



  • @mrjj about the loop: i tested it with a lot of different lengths of text and by changing font and font size. Most of the time it was fine, but let's say about 20% chance of a failure/loop. I did use a lot of insertHtml's. One for every line, so that would be a lot of changes in document size.

    Perhaps the possibility of such a loop is why the functionality is not available by default...?


Log in to reply