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 minimal height but expanding problem
Forum Updated to NodeBB v4.3 + New Features

QTextEdit minimal height but expanding problem

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 5 Posters 16.6k Views 4 Watching
  • 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 Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by SGaist
    #8

    Hi,

    IIRC, you should add vLayout.addStretch() before you add your combobox to the layout.

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

    JonBJ 1 Reply Last reply
    0
    • SGaistS SGaist

      Hi,

      IIRC, you should add vLayout.addStretch() before you add your combobox to the layout.

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #9

      @SGaist
      I want the bottom of the QTextEdit to be immediately followed by the QComboBox. (The vLayout.addStretch() I presently have is for the gap from the QComboBox down to the QPushButton.) You want me to put a vLayout.addStretch() between the QTextEditand the QComboBox, even though I want them touching? I will certainly try whatever you suggest tomorrow!

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

        Sorry, I misunderstood you. Therefor no, my answer was wrong.

        In that case, why add a stretch add all ?

        If you want your QTextEdit to take most of the space, then set the stretch parameter of the addWidget call to 1 and it should do what you want.

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

        JonBJ 1 Reply Last reply
        0
        • SGaistS SGaist

          Sorry, I misunderstood you. Therefor no, my answer was wrong.

          In that case, why add a stretch add all ?

          If you want your QTextEdit to take most of the space, then set the stretch parameter of the addWidget call to 1 and it should do what you want.

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #11

          @SGaist
          No, the whole point of this seems to boil down to: I want the QTextEdit to take the least vertical space to display its content. (E.g. in he second screenshot, I want the combobox up to just below where the text ends, not down near the button.) And that is not proving possible?

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #12

            Just to be sure.
            The goal is like this

            alt text

            This is via code.
            Setting Layout margins to zero and disable scrollbars.
            Then controlling max height via code. Not super elegant.
            Im very interested if this can be done with Policy alone.

            void MainWindow::on_textEdit_textChanged() {
              QSize size = ui->textEdit->document()->size().toSize();
              ui->textEdit->setFixedHeight( size.height()  );
            }
            
            

            Using a spacer i force the button to stay below.
            alt text

            JonBJ 1 Reply Last reply
            3
            • mrjjM mrjj

              Just to be sure.
              The goal is like this

              alt text

              This is via code.
              Setting Layout margins to zero and disable scrollbars.
              Then controlling max height via code. Not super elegant.
              Im very interested if this can be done with Policy alone.

              void MainWindow::on_textEdit_textChanged() {
                QSize size = ui->textEdit->document()->size().toSize();
                ui->textEdit->setFixedHeight( size.height()  );
              }
              
              

              Using a spacer i force the button to stay below.
              alt text

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by JonB
              #13

              @mrjj
              Exactly!!

              In my case, I am only using the QTextEdit as a read-only for displaying a message of unknown length. Therefore personally I do not need it to resize as the user types, only when created or via setText(). I can apply your principle then.

              I didn't expect to have to do this by setting fixed height in code. Like you, I think, I expected this to be doable purely by some size policy. All I seem to need is a QTextEdit which can grow or shrink as required for its content, but QSizePolicy is not allowing this? A QLabel with no size specified takes up as much room as its text, right? Can I get a (read-only will suffice) QTextEdit to do that?

              J.HilkJ 1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #14

                Try with that version:

                dlg = QtWidgets.QDialog()
                dlg.setWindowTitle("Dialog")
                dlg.setGeometry(50, 50, 300, 300)
                
                vLayout = QtWidgets.QVBoxLayout(dlg)
                vLayout.setContentsMargins(0, 0, 0, 0)
                vLayout.setSpacing(0)
                
                te = QtWidgets.QTextEdit()
                te.setText("This is some message text\nacross two lines.")
                te.setReadOnly(True)
                te.setStyleSheet("background-color: red;")
                vLayout.addWidget(te)
                
                cmb = QtWidgets.QComboBox()
                cmb.addItem("ComboBox")
                vLayout.addWidget(cmb)
                
                vLayout.addStretch(1)
                
                btn = QtWidgets.QPushButton("Button")
                vLayout.addWidget(btn)
                
                font = te.document().defaultFont() 
                fontMetrics = QtGui.QFontMetrics(font) 
                textSize = fontMetrics.size(0, te.toPlainText())
                textHeight = textSize.height() + 30 # Need to tweak
                te.setMaximumHeight(textHeight) 
                

                You may have to tweak it a bit further. Likely redo the height calculation when you set a new text.

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

                JonBJ 1 Reply Last reply
                1
                • JonBJ JonB

                  @mrjj
                  Exactly!!

                  In my case, I am only using the QTextEdit as a read-only for displaying a message of unknown length. Therefore personally I do not need it to resize as the user types, only when created or via setText(). I can apply your principle then.

                  I didn't expect to have to do this by setting fixed height in code. Like you, I think, I expected this to be doable purely by some size policy. All I seem to need is a QTextEdit which can grow or shrink as required for its content, but QSizePolicy is not allowing this? A QLabel with no size specified takes up as much room as its text, right? Can I get a (read-only will suffice) QTextEdit to do that?

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #15

                  @JonB said in QTextEdit minimal height but expanding problem:

                  In my case, I am only using the QTextEdit as a read-only for displaying a message of unknown length. Therefore personally I do not need it to resize as the user types, only when created or via setText(). I can apply your principle then.

                  If it's read only, why don't you use a QLabel than? fixes all your problems.

                  Otherwise, I would probably subclass QTextEdit and overwrite the sizeHint method.


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  JonBJ 1 Reply Last reply
                  0
                  • J.HilkJ J.Hilk

                    @JonB said in QTextEdit minimal height but expanding problem:

                    In my case, I am only using the QTextEdit as a read-only for displaying a message of unknown length. Therefore personally I do not need it to resize as the user types, only when created or via setText(). I can apply your principle then.

                    If it's read only, why don't you use a QLabel than? fixes all your problems.

                    Otherwise, I would probably subclass QTextEdit and overwrite the sizeHint method.

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by
                    #16

                    @J.Hilk

                    If it's read only, why don't you use a QLabel than? fixes all your problems.

                    In my original question I wrote:

                    (Can't remember why, but I want a QTextEdit, not a QLabel.)

                    ! :)

                    But since you ask :) :

                    • Can a QLabel put in scrollbars if required if the height is too big? I don't think so. You will recall I have said when I have got it working I will actually limit the height of the textedit to, say, 100px, and then rely on its scrolling behaviour for the user to be able to read the full text. This may be required because the dialog is generic, and might be used to display, say, a very long error text detail returned from somewhere.

                    • The app's stylesheets have a common rule for the display of QTextEdits which is suited to such read-only messages. I would have to write a separate rule for this case if I changed it to QLabel, or change all other occurrences in code where a QTextEdit is used in this situation.

                    • The end user may need to copy such (error) messages, e.g. for sending to support in an email. This is easy in a QTextEdit. Does a QLabel allow such easy select & paste? (I think you have to specify an option on QLabel to allow that.)

                    1 Reply Last reply
                    0
                    • SGaistS SGaist

                      Try with that version:

                      dlg = QtWidgets.QDialog()
                      dlg.setWindowTitle("Dialog")
                      dlg.setGeometry(50, 50, 300, 300)
                      
                      vLayout = QtWidgets.QVBoxLayout(dlg)
                      vLayout.setContentsMargins(0, 0, 0, 0)
                      vLayout.setSpacing(0)
                      
                      te = QtWidgets.QTextEdit()
                      te.setText("This is some message text\nacross two lines.")
                      te.setReadOnly(True)
                      te.setStyleSheet("background-color: red;")
                      vLayout.addWidget(te)
                      
                      cmb = QtWidgets.QComboBox()
                      cmb.addItem("ComboBox")
                      vLayout.addWidget(cmb)
                      
                      vLayout.addStretch(1)
                      
                      btn = QtWidgets.QPushButton("Button")
                      vLayout.addWidget(btn)
                      
                      font = te.document().defaultFont() 
                      fontMetrics = QtGui.QFontMetrics(font) 
                      textSize = fontMetrics.size(0, te.toPlainText())
                      textHeight = textSize.height() + 30 # Need to tweak
                      te.setMaximumHeight(textHeight) 
                      

                      You may have to tweak it a bit further. Likely redo the height calculation when you set a new text.

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by JonB
                      #17

                      @SGaist
                      Yep, what you suggest does "work". But like @mrjj's solution it requires code to calculate the text height and then sets the QTextEdits maximum or fixed height.

                      @mrjj & I are "surprised" that there does not seem to be any QSizePolicy or flag which can make QTextEdit occupy the minimum vertical size necessary for its content, like, say, QLabel does. Is that indeed right? QTextEdit seems to have some in-built minimal height? Is that right, is that documented?

                      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