[Interest] Layout on a QScrollArea not properly downsized when changed off-screen

Bo Thorsen bthorsen at ics.com
Sat Jun 15 10:15:25 CEST 2013


Den 14-06-2013 12:12, Alejandro Exojo skrev:
> Hi.
>
> I've implemented a kind of vertical list of widgets with a class based
> on QScrollArea. The main widget has QVBoxLayout as layout. I have a
> method to clear the whole list and empty the layout, and another to add
> one widget to the layout.
>
> The problem I'm having is that I clear and fill the layout both with the
> widget visible and not visible (the list is in a QDialog, so I have to
> fill it before I call exec()). When the list/layout contents change with
> the widget visible, everything is fine. But if I do it before the
> exec(), so before is shown, the layout only grows if previously had less
> items, but it never shrinks if I empty it and add less items than
> previously.
>
> I'm doing the following to empty the list, where m_list is a QList with
> pointers to the elements added and m_layout is the layout of the
> widget() of the QScrollArea:
>
> void List::clear()
> {
>      while (!m_list.empty()) {
>          QWidget* widget = m_list.takeLast();
>          m_layout->removeWidget(widget);
>          delete widget;
>      }
> }
>
> And the following to add widgets:
>
> void List::addWidget(QWidget* item)
> {
>      // This widget draws a left-bottom-right frame.
>      QWidget* frame = new QWidget(this);
>      frame->setStyleSheet(".QWidget {"
>          "border: 3px solid rgb(6, 78, 125);"
>          "border-top: 0px;"
>          "}"
>      );
>      // Wrapper layout: the frame widget has inside the interesting
> contents (another widget)
>      QVBoxLayout* wrap = new QVBoxLayout();
>      wrap->setMargin(0);
>      wrap->setSpacing(0);
>      wrap->addWidget(item);
>      frame->setLayout(wrap);
>      frame->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
>
>      frame->installEventFilter(this);
>      m_list.append(frame);
>      m_layout->addWidget(frame);
> }
>
>
> I've also put the main widget in an event filter, and I've seen is
> receiving Resize events when calling addWidget with the widget visible,
> but no resize events at all before is shown.
>
> I've also tried to call update() and repaint() in layouts and widgets
> here and there, but no change at all.

Hi Alejandro,

Try calling invalidate() on the layout or updateGeometry() on the 
container widget.

Bo.

-- 
Bo Thorsen, European Qt Manager, Integrated Computer Solutions
ICS - Delivering World-Class Applications for Embedded & Mobile Devices
http://ics.com/services



More information about the Interest mailing list