Child widget not updating parent widget layout



  • How can I force parent widgets to update their layout when a child layout changes its size hints? I have sizeHints overriden, and i call updateGeometry(), but none of these work. The layout updates only after a resizeEvent is called (which calls my updateLayout() function). Below is a full test case showing 3 layouts stacked on top of each other, and pressing button adds 50 pixels to teh top widget's height. (this is a test case, and i'm aware of existing QLayouts).

     #ifndef WIDGET_H_
     #define WIDGET_H_
     
     #include <QWidget>
     #include <QColor>
     #include <QPalette>
     #include <QPushButton>
     
     class SubWidget : public QWidget
     {
       Q_OBJECT
     public:
       SubWidget(QColor const& c, QWidget *parent = nullptr)
         : QWidget(parent), addedHeight_(0)
       {
         QPalette pal(palette());
         pal.setColor(QPalette::Background, c);
         setAutoFillBackground(true);
         setPalette(pal);
       }
     
       virtual QSize sizeHint() const override
       {
         return QSize(100, 50+addedHeight_);
       }
     
       virtual QSize minimumSizeHint() const override
       {
         return sizeHint();
       }
     
     public slots:
       void addFiftyPixelsToHeight()
       {
         addedHeight_ += 50; 
         updateGeometry();
         update();
       }
       
     private:
       int addedHeight_;
     };
     
     
     class MainWidget : public QWidget
     {
       Q_OBJECT
     public:
       explicit MainWidget(QWidget *parent = nullptr)
         : QWidget(parent)
       {
         topWidget_ = new SubWidget(QColor(Qt::red), this);
         botWidget_ = new SubWidget( QColor(Qt::yellow), this);
         button_    = new QPushButton(this);
     
         connect(button_, SIGNAL(clicked()), topWidget_, SLOT(addFiftyPixelsToHeight()));
         resize(200,300);
       }
     
       virtual void showEvent(QShowEvent *event) override
       {
         QWidget::showEvent(event);
       }
     
       virtual void resizeEvent(QResizeEvent *event) override
       {
         updateLayout();
         QWidget::resizeEvent(event);
       }
     
     private:
     
       // Update the layout of the widgets by stacking them on top of each other
       void updateLayout()
       {
         QRect const r = geometry();
     
         QSize const topSize    = topWidget_->sizeHint();
         QSize const bottomSize = botWidget_->sizeHint();
     
         QRect const topRect    = QRect(0,0, r.width(), topSize.height());
         QRect const bottomRect = QRect(0,topSize.height(), r.width(), bottomSize.height());
         QRect const buttonRect = QRect(0,topSize.height()+bottomSize.height(), 
                                        r.width(), r.height()-topSize.height()-bottomSize.height());
         topWidget_->setGeometry(topRect);
         botWidget_->setGeometry(bottomRect);
         button_->setGeometry(buttonRect);
       }
     
     
       SubWidget *topWidget_;
       SubWidget *botWidget_;
       QPushButton *button_;
     };
     
     
     
     
     #endif /* WIDGET_H_ */
     
     
     int main(int argc, char *argv[])
     {
       QApplication app(argc, argv);
     
       MainWidget w;
       w.show();
       
       return app.exec();
     }

  • Qt Champions 2016

    Hi and welcome
    Have you tried with
    QSizePolicy::Expanding
    void QSizePolicy::setHorizontalPolicy(Policy policy)

    so its set to use as much as possible.



  • @mrjj Yes. For this test case, I believe it would be setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding), but it does not work.


Log in to reply
 

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