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.
  • T Offline
    T Offline
    Tink
    wrote on 19 May 2018, 13:57 last edited by
    #1

    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?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 19 May 2018, 20:35 last edited by
      #2

      Hi,

      Is it going to be read-only text ?

      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 19 May 2018, 21:55
      0
      • S SGaist
        19 May 2018, 20:35

        Hi,

        Is it going to be read-only text ?

        T Offline
        T Offline
        Tink
        wrote on 19 May 2018, 21:55 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
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 20 May 2018, 20:39 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 21 May 2018, 17:15
          0
          • S SGaist
            20 May 2018, 20:39

            Then why not use QLabel ?

            T Offline
            T Offline
            Tink
            wrote on 21 May 2018, 17:15 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
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 21 May 2018, 21:38 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 20 Aug 2018, 11:20
              0
              • S SGaist
                21 May 2018, 21:38

                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 20 Aug 2018, 11:20 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...

                M 1 Reply Last reply 20 Aug 2018, 15:26
                0
                • T Tink
                  20 Aug 2018, 11:20

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

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 20 Aug 2018, 15:26 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 20 Aug 2018, 21:17 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!

                    M 1 Reply Last reply 21 Aug 2018, 10:32
                    0
                    • T Offline
                      T Offline
                      Tink
                      wrote on 21 Aug 2018, 10:25 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
                        20 Aug 2018, 21:17

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

                        M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 21 Aug 2018, 10:32 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 21 Aug 2018, 10:52
                        0
                        • M mrjj
                          21 Aug 2018, 10:32

                          @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 21 Aug 2018, 10:52 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