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 17.2k 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.
  • JonBJ JonB

    @kenchan
    Sorry, I don't understand. Python is not the issue. I do not have Qt Creator to try/test anything. I'll add whatever you suggest in code (C++ is fine), so please tell me what you want added where? But if I add just a QSpacer (I already have a QStretch there with the vLayout.addStretch()), I don't see how that will address the behaviour of QSizePolicy.MinimumExpanding? (BTW, I asked a "friendly expert" to achieve this in Qt Creator however he liked, but he couldn't get the desired behaviour. We're both unsure how it should be achieved.)

    K Offline
    K Offline
    kenchan
    wrote on last edited by
    #4

    @JonB

    OK, is this the kind of thing you want?

    /********************************************************************************
    ** Form generated from reading UI file 'dialog.ui'
    **
    ** Created by: Qt User Interface Compiler version 5.9.2
    **
    ** WARNING! All changes made in this file will be lost when recompiling UI file!
    ********************************************************************************/
    
    #ifndef UI_DIALOG_H
    #define UI_DIALOG_H
    
    #include <QtCore/QVariant>
    #include <QtWidgets/QAction>
    #include <QtWidgets/QApplication>
    #include <QtWidgets/QButtonGroup>
    #include <QtWidgets/QComboBox>
    #include <QtWidgets/QDialog>
    #include <QtWidgets/QHeaderView>
    #include <QtWidgets/QPushButton>
    #include <QtWidgets/QSpacerItem>
    #include <QtWidgets/QTextEdit>
    #include <QtWidgets/QVBoxLayout>
    
    QT_BEGIN_NAMESPACE
    
    class Ui_Dialog
    {
    public:
        QVBoxLayout *verticalLayout_2;
        QVBoxLayout *verticalLayout;
        QTextEdit *textEdit;
        QComboBox *comboBox;
        QSpacerItem *verticalSpacer;
        QPushButton *pushButton;
    
        void setupUi(QDialog *Dialog)
        {
            if (Dialog->objectName().isEmpty())
                Dialog->setObjectName(QStringLiteral("Dialog"));
            Dialog->resize(354, 320);
            verticalLayout_2 = new QVBoxLayout(Dialog);
            verticalLayout_2->setSpacing(6);
            verticalLayout_2->setContentsMargins(11, 11, 11, 11);
            verticalLayout_2->setObjectName(QStringLiteral("verticalLayout_2"));
            verticalLayout = new QVBoxLayout();
            verticalLayout->setSpacing(6);
            verticalLayout->setObjectName(QStringLiteral("verticalLayout"));
            textEdit = new QTextEdit(Dialog);
            textEdit->setObjectName(QStringLiteral("textEdit"));
            textEdit->setMinimumSize(QSize(0, 100));
            textEdit->setMaximumSize(QSize(16777215, 100));
    
            verticalLayout->addWidget(textEdit);
    
            comboBox = new QComboBox(Dialog);
            comboBox->setObjectName(QStringLiteral("comboBox"));
    
            verticalLayout->addWidget(comboBox);
    
            verticalSpacer = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
    
            verticalLayout->addItem(verticalSpacer);
    
            pushButton = new QPushButton(Dialog);
            pushButton->setObjectName(QStringLiteral("pushButton"));
    
            verticalLayout->addWidget(pushButton);
    
    
            verticalLayout_2->addLayout(verticalLayout);
    
    
            retranslateUi(Dialog);
    
            QMetaObject::connectSlotsByName(Dialog);
        } // setupUi
    
        void retranslateUi(QDialog *Dialog)
        {
            Dialog->setWindowTitle(QApplication::translate("Dialog", "Dialog", Q_NULLPTR));
            pushButton->setText(QApplication::translate("Dialog", "PushButton", Q_NULLPTR));
        } // retranslateUi
    
    };
    
    namespace Ui {
        class Dialog: public Ui_Dialog {};
    } // namespace Ui
    
    QT_END_NAMESPACE
    
    #endif // UI_DIALOG_H
    
    JonBJ 1 Reply Last reply
    0
    • K kenchan

      @JonB

      OK, is this the kind of thing you want?

      /********************************************************************************
      ** Form generated from reading UI file 'dialog.ui'
      **
      ** Created by: Qt User Interface Compiler version 5.9.2
      **
      ** WARNING! All changes made in this file will be lost when recompiling UI file!
      ********************************************************************************/
      
      #ifndef UI_DIALOG_H
      #define UI_DIALOG_H
      
      #include <QtCore/QVariant>
      #include <QtWidgets/QAction>
      #include <QtWidgets/QApplication>
      #include <QtWidgets/QButtonGroup>
      #include <QtWidgets/QComboBox>
      #include <QtWidgets/QDialog>
      #include <QtWidgets/QHeaderView>
      #include <QtWidgets/QPushButton>
      #include <QtWidgets/QSpacerItem>
      #include <QtWidgets/QTextEdit>
      #include <QtWidgets/QVBoxLayout>
      
      QT_BEGIN_NAMESPACE
      
      class Ui_Dialog
      {
      public:
          QVBoxLayout *verticalLayout_2;
          QVBoxLayout *verticalLayout;
          QTextEdit *textEdit;
          QComboBox *comboBox;
          QSpacerItem *verticalSpacer;
          QPushButton *pushButton;
      
          void setupUi(QDialog *Dialog)
          {
              if (Dialog->objectName().isEmpty())
                  Dialog->setObjectName(QStringLiteral("Dialog"));
              Dialog->resize(354, 320);
              verticalLayout_2 = new QVBoxLayout(Dialog);
              verticalLayout_2->setSpacing(6);
              verticalLayout_2->setContentsMargins(11, 11, 11, 11);
              verticalLayout_2->setObjectName(QStringLiteral("verticalLayout_2"));
              verticalLayout = new QVBoxLayout();
              verticalLayout->setSpacing(6);
              verticalLayout->setObjectName(QStringLiteral("verticalLayout"));
              textEdit = new QTextEdit(Dialog);
              textEdit->setObjectName(QStringLiteral("textEdit"));
              textEdit->setMinimumSize(QSize(0, 100));
              textEdit->setMaximumSize(QSize(16777215, 100));
      
              verticalLayout->addWidget(textEdit);
      
              comboBox = new QComboBox(Dialog);
              comboBox->setObjectName(QStringLiteral("comboBox"));
      
              verticalLayout->addWidget(comboBox);
      
              verticalSpacer = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
      
              verticalLayout->addItem(verticalSpacer);
      
              pushButton = new QPushButton(Dialog);
              pushButton->setObjectName(QStringLiteral("pushButton"));
      
              verticalLayout->addWidget(pushButton);
      
      
              verticalLayout_2->addLayout(verticalLayout);
      
      
              retranslateUi(Dialog);
      
              QMetaObject::connectSlotsByName(Dialog);
          } // setupUi
      
          void retranslateUi(QDialog *Dialog)
          {
              Dialog->setWindowTitle(QApplication::translate("Dialog", "Dialog", Q_NULLPTR));
              pushButton->setText(QApplication::translate("Dialog", "PushButton", Q_NULLPTR));
          } // retranslateUi
      
      };
      
      namespace Ui {
          class Dialog: public Ui_Dialog {};
      } // namespace Ui
      
      QT_END_NAMESPACE
      
      #endif // UI_DIALOG_H
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #5

      @kenchan said in QTextEdit minimal height but expanding problem:

      textEdit->setMinimumSize(QSize(0, 100));
      textEdit->setMaximumSize(QSize(16777215, 100));

      That looks like you minimum height is the same as your maximum height at value 100, no? Shall I just take that as a typo for 20 as minimum?

      If I understand right, all you are changing from mine is:

      • Get rid of my te.setSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.MinimumExpanding) line completely, leave it at default.

      • Replace my vLayout.addStretch() with your QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);

      Is that it? (I tried that principle and it made no difference.) Or, do I see you're utilising two QVBoxLayouts to achieve something, and that's what the difference is?

      K 1 Reply Last reply
      0
      • JonBJ JonB

        @kenchan said in QTextEdit minimal height but expanding problem:

        textEdit->setMinimumSize(QSize(0, 100));
        textEdit->setMaximumSize(QSize(16777215, 100));

        That looks like you minimum height is the same as your maximum height at value 100, no? Shall I just take that as a typo for 20 as minimum?

        If I understand right, all you are changing from mine is:

        • Get rid of my te.setSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.MinimumExpanding) line completely, leave it at default.

        • Replace my vLayout.addStretch() with your QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);

        Is that it? (I tried that principle and it made no difference.) Or, do I see you're utilising two QVBoxLayouts to achieve something, and that's what the difference is?

        K Offline
        K Offline
        kenchan
        wrote on last edited by
        #6

        @JonB
        You can use the sizes you need. You can probably get away with only the one vertical layout. Does it behave something like you want?

        JonBJ 1 Reply Last reply
        0
        • K kenchan

          @JonB
          You can use the sizes you need. You can probably get away with only the one vertical layout. Does it behave something like you want?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #7

          @kenchan
          I appreciate your example. To test it I will have to translate all the code into Python. Of course I can do that, but not in one minute! I cannot see how it would address the problem I am facing (if all you are doing is replacing my QStretch with your QSpacer, which I have tested) , so I'm not keen to start out on the full translation yet, though I wouldn't want you to be insulted! If you looked at/pasted a screenshot with one the one hand one line of text in the text edit and on the other hand six lines of text in the text edit we would know immediately whether it looked right...!

          1 Reply Last reply
          0
          • 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 Offline
              JonBJ Offline
              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 Offline
                  JonBJ Offline
                  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 Offline
                      JonBJ Offline
                      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 Online
                          J.HilkJ Online
                          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 Offline
                            JonBJ Offline
                            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 Offline
                              JonBJ Offline
                              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