QLabel won't expand when in QScrollArea



  • I display a QLabel, and when it is not inside a QScrollArea, doing setText() makes it expand vertically to accommodate the text. But when it is in a QScrollArea, it won't expand. How do I get it to expand?

    @// this is in the constructor of a class derived from QWidget
    // inspect is a class variable also referenced in a slot
    QFormLayout *layout = new QFormLayout;
    // ... other QWidgets added into layout, mostly "string" and QLineEdit pairs.
    inspect = new QLabel("");
    layout->addRow("All Macros:",inspect);
    setLayout(layout);@

    Later when a slot does inspect->setText(--multi-line-text--) the QLabel expands properly.

    But replace that last line with a QScrollArea:
    @QWidget *w = new QWidget;
    w->setLayout(layout);
    QScrollArea *sa = new QScrollArea;
    sa->setWidget(w);
    QSizePolicy sizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    sizePolicy.setHorizontalStretch(1);
    sizePolicy.setVerticalStretch(1);
    sa->setSizePolicy(sizePolicy);
    layout = new QFormLayout;
    layout->addRow(sa);
    setLayout(layout);
    @

    And now after the slot's setText() it only shows a fractured image of parts of the first two lines. This is independent of the scroll-bar positions. The QScrollArea does properly expand to fill the parent widget as the parent is resized, and the scroll bars appear appropriately. But the inspect QLabel displays garbage -- how do I fix that?

    BTW I am using QT 4.8, as I am using the Coin3D SoQt viewer, and that is the latest version of QT it supports.



  • When using QScrollArea you have to set minimum size (if not implementing sizeHint()).

    so your code should be
    @QWidget *w = new QWidget;
    w->setLayout(layout);
    w->setMinimumSize(800, 500); // It is necessary
    QScrollArea *sa = new QScrollArea;
    sa->setWidget(w);
    layout = new QFormLayout;
    layout->addRow(sa);
    setLayout(layout);@



  • Thanks, but that did not help. Nor did calling setMinimumSize() on the QLabel itself. Nor did calling setSizePolicy() on the QLabel with the same sizePolicy in my original post.

    I'm, stumped -- I really need to display arbitrarily-sized text in the widget inside the QScrollArea. Is there some other widget that might do? Perhaps something corresponding to a disabled HTML <textarea>? Note that QLabel does this fine when NOT inside a QScrollArea -- what has changed its behavior?

    The parent of the QScrollArea is a vertical QSplitter, which is inside a horizontal QSplitter. So the user can vary the size available to the QScrollArea; hence the need for scroll bars. The left panel of the horizontal QSplitter is a SoQt viewer displaying a 3-d system; on the right, the widgets and their content need to dynamically change as the user selects or edits different objects in the 3-d viewer.



  • I wrote a little function to print all of the properties of the QLabel, hoping to see the difference between when it is in a QScrollArea, and not. Only x, y, and height differed. There was no difference in the properties of the layout.

    So I think this is not going to work.

    I switched to a QTextEdit with setReadOnly(true). That is clunkier, and does not auto-resize to its text, but at least when there's too much text it applies scroll bars rather than truncating.



  • QScrollArea will show the scroll bars if the QScrollArea is smaller than the viewport widget size and hides the scroll bars when the QScrollArea is larger than the viewport widget, now here when the QScrollArea is larger than widget it will only hide the scroll bars and will not increase the size the widget. The size of the viewport widget has to set by the user, QScrollArea cannot decide on this and hence will not change the size the widget.

    In other words QScrollArea will only show/hide the scrollbars as needed and WILL NOT change the size the viewport widget, viewport widget size has to adjusted explicitly either using setMinimumSize() or using sizeHint().


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.