Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

QTextEdit minimal height but expanding problem



  • I am having real trouble with a QTextEdit, trying to get it to have a minimal height but expanding.

    I use Python/PyQt, and have to create all my widgets in code. I can't use Qt Creator to play with stuff, and I can't accept a Qt Creator solution. This really ought not be so difficult, but read on....

    I want:

    • A generic QDialog, laid out vertically.
    • A read-only QTextEdit at the top to hold a potentially multi-line message. (Can't remember why, but I want a QTextEdit, not a QLabel.)
    • Then a (vertical) gap.
    • Then a button at the bottom.

    I inherited the following code producing that:

        dlg = QtWidgets.QDialog(main)
        dlg.setWindowTitle("Dialog")
        dlg.setGeometry(50, 50, 300, 300)
    
        vLayout = QtWidgets.QVBoxLayout(dlg)
    
        te = QtWidgets.QTextEdit()
        te.setText("This is some message text\nacross two lines.")
        te.setReadOnly(True)
        te.setStyleSheet("background-color: transparent;")
        te.setSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.MinimumExpanding)
        vLayout.addWidget(te)
    
        vLayout.addStretch()
    
        btn = QtWidgets.QPushButton("Button")
        vLayout.addWidget(btn)
    

    0_1541601201838_screenshot1.png

    Now, I need (optionally) to allow for another widget (a combobox) to come below the textedit and above the button. But note: the combobox must be placed immediately below the textedit (the text refers to it), then the gap to the button at the bottom; not the gap then the combo followed by the bottom.

        dlg = QtWidgets.QDialog(main)
        dlg.setWindowTitle("Dialog")
        dlg.setGeometry(50, 50, 300, 300)
    
        vLayout = QtWidgets.QVBoxLayout(dlg)
    
        te = QtWidgets.QTextEdit()
        te.setText("This is some message text\nacross two lines.")
        te.setReadOnly(True)
        te.setStyleSheet("background-color: transparent;")
        te.setSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.MinimumExpanding)
        vLayout.addWidget(te)
    
        cmb = QtWidgets.QComboBox()
        cmb.addItem("ComboBox")
        vLayout.addWidget(cmb)
    
        vLayout.addStretch()
    
        btn = QtWidgets.QPushButton("Button")
        vLayout.addWidget(btn)
    

    0_1541601225125_screenshot2.png screenshot2

    That is no good, because the combobox needs to be just below the textedit. You can see it clearer if you comment out the setStylesheet(transparent) line in the code.

    This comes about because of the behaviour of QtWidgets.QSizePolicy.MinimumExpanding choosing to occupy as much room as possible, I want as little as possible.

    Now, it's way too much for me to detail everything I have tried, but I have spent a lot of time and tried a lot of things!

    I must have it so the text can be one line or many lines, with the textbox only taking as much as it needs.

    As an "optional extra would be nice", if we could set the textedit's maximum to, say, 100, and then it would start using vertical scrollbar, that would cater for this generic dialog being used for a potentially very long message. I'd like that as well, but not if stops/complicates the solution. (This one may turn out to work with just a QTextEdit::setMaximumHeight(), depending on your solution, I don't know.)

    I'd be so grateful for some help here, I'm stuck... :(

    P.S.
    In one of my many iterations, I tried settling for:

    te.setMinimumHeight(20)
    te.setMaximumHeight(100)
    te.setSizePolicy(QtWidgets.QSizePolicy.Preferred, ???)
    

    I'd settle for that if necessary, but no matter what I tried for the ??? it always occupied the maximum height, never the minimum. It was suggested that this was because the textedit is inside a QVBoxLayout, but even fiddling with the stretch parameter to addWidget() it still didn't work.



  • @JonB
    Can you not add a vertical spacer between the combobox and the button?
    Even though you cannot use the designer in QtCreator to help you write your code. You could just use it to layout a mockup dialog using the layouts etc. and study the resulting c++ code. Then maybe you can see the relationships and translate that to your Python?



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



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


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



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



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


  • Lifetime Qt Champion

    Hi,

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



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


  • Lifetime Qt Champion

    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.



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


  • Lifetime Qt Champion

    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



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


  • Lifetime Qt Champion

    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.


  • Moderators

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



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



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


Log in to reply