Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTextEdit resize on content change?
QtWS25 Last Chance

QTextEdit resize on content change?

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 7.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SGaistS SGaist

    Hi,

    Is it going to be read-only text ?

    T Offline
    T Offline
    Tink
    wrote on last edited by
    #3

    @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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #4

      Then why not use QLabel ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      T 1 Reply Last reply
      0
      • SGaistS SGaist

        Then why not use QLabel ?

        T Offline
        T Offline
        Tink
        wrote on last edited by
        #5

        @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?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #6

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          T 1 Reply Last reply
          0
          • SGaistS SGaist

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

            T Offline
            T Offline
            Tink
            wrote on last edited by
            #7

            @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...

            mrjjM 1 Reply Last reply
            0
            • T Tink

              @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...

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #8

              @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

              1 Reply Last reply
              4
              • T Offline
                T Offline
                Tink
                wrote on last edited by
                #9

                @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!

                mrjjM 1 Reply Last reply
                0
                • T Offline
                  T Offline
                  Tink
                  wrote on last edited by
                  #10

                  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.

                  1 Reply Last reply
                  0
                  • T Tink

                    @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!

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #11

                    @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.

                    T 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      @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.

                      T Offline
                      T Offline
                      Tink
                      wrote on last edited by
                      #12

                      @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...?

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved