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. Child widget not updating parent widget layout
Forum Updated to NodeBB v4.3 + New Features

Child widget not updating parent widget layout

Scheduled Pinned Locked Moved Unsolved General and Desktop
widgetgeometrylayoutresize
3 Posts 2 Posters 4.3k Views 1 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.
  • J Offline
    J Offline
    jj382
    wrote on 24 Nov 2015, 03:46 last edited by
    #1

    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();
     }
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 24 Nov 2015, 09:15 last edited by
      #2

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

      so its set to use as much as possible.

      J 1 Reply Last reply 24 Nov 2015, 13:50
      0
      • M mrjj
        24 Nov 2015, 09:15

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

        so its set to use as much as possible.

        J Offline
        J Offline
        jj382
        wrote on 24 Nov 2015, 13:50 last edited by
        #3

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

        1 Reply Last reply
        0

        1/3

        24 Nov 2015, 03:46

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved