Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-66151

QGridLayout not resized nor repainted when child widgets get hidden

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P3: Somewhat important
    • None
    • 5.10.0
    • Widgets: Layout
    • None
    • Windows 10 64bit

    Description

      I have a widget with `QVBoxLayout` as the main layout and `QGridLayout` as a child layout nested inside the main one. There is toggle button which shows/hides certain widgets in the grid layout. After updating visibility I want to adjust (minimize) the size of the widget. But resizing does not work correctly when child widgets get hidden, the widget size stays the same, it is not shrinked as I would expect. Moreover the widget is not correctly repainted, some bad button fragments remain visible. I observed that this does not happen if the grid layout is the main one (i.e. there is no vertical layout), but if it is nested inside another layout it behaves strange. I suspect that QFormLayout behaves the same.

      Before: https://i.stack.imgur.com/So99g.png
      After clicking 2 times on the button: https://i.stack.imgur.com/iDf63.png

      widget.h:

      #pragma once
      
      #include <QWidget>
      
      class QGridLayout;
      class QPushButton;
      
      class Widget : public QWidget
      {
          Q_OBJECT
      public:
          explicit Widget(QWidget *parent = 0);
      private:
          void updateVisibility();
          QGridLayout *m_layout;
          QPushButton *m_button;
      };
      

      widget.cpp:

      #include "widget.h"
      
      #include <QGridLayout>
      #include <QLabel>
      #include <QPushButton>
      
      Widget::Widget(QWidget *parent) :
          QWidget(parent)
      {
          m_layout = new QGridLayout();
          m_layout->addWidget(new QLabel("Label1"), 0, 0);
          m_layout->addWidget(new QLabel("Label2"), 1, 0);
      
          m_button = new QPushButton("Visible");
          connect(m_button, &QPushButton::clicked, this, &Widget::updateVisibility);
          m_button->setCheckable(true);
          m_button->setChecked(false);
      
          auto layout = new QVBoxLayout(this);
          layout->addLayout(m_layout);
          layout->addWidget(m_button);
          updateVisibility();
      }
      
      void Widget::updateVisibility()
      {
          // hide or show Label2
          m_layout->itemAtPosition(1, 0)->widget()->setVisible(m_button->isChecked());
          adjustSize();
      }
      

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            vladimir.kraus Vladimir Kraus
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes