Search code examples
c++qtwidgetqlayout

Qt viewport widget is not expanding during new child widgets addition to QBoxLayout


I want to create a scrollable widget which contains a collection of child widgets managed by QBoxLayout, and it must be easy to add and remove widgets from this collection. But when I add child widgets to it, viewport widget did not expand its size (remain initial size), instead children are overlapping on themselves. I do not know what to do to fix this.

Here is the code:

mainwidget.h

#ifndef MAINWIDGET_H
#define MAINWIDGET_H

#include <QtGui/QScrollArea>
#include <QResizeEvent>
#include <QDebug>

class CMainWidget : public QScrollArea
{
    Q_OBJECT
    
public:
    CMainWidget(QWidget *parent = 0);
    ~CMainWidget();

protected:
    virtual void resizeEvent(QResizeEvent *pEvent);
    virtual void keyPressEvent(QKeyEvent *pEvent);
};

#endif // MAINWIDGET_H

mainwidget.cpp

#include "mainwidget.h"
#include "rootitem.h"

CMainWidget::CMainWidget(QWidget *parent)
    : QScrollArea(parent)
{
    QWidget* pViewport = new QWidget();
    QBoxLayout* pLayout = new QBoxLayout(QBoxLayout::TopToBottom);

    pLayout->setSizeConstraint(QLayout::SetNoConstraint);

    for (int iWidgetIndex = 0; iWidgetIndex < 20; iWidgetIndex++)
        pLayout->addWidget(new CRootItem());

    pViewport->setLayout(pLayout);
    pViewport->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::MinimumExpanding);

    setWidget(pViewport);
}

CMainWidget::~CMainWidget()
{
    
}


void CMainWidget::resizeEvent(QResizeEvent *pEvent)
{
    QWidget* pViewport = widget();

    int iHeight = pViewport->height();
    int iWidth = pEvent->size().width();

    pViewport->resize(iWidth, iHeight);
}


void CMainWidget::keyPressEvent(QKeyEvent *pEvent)
{
    QWidget* pViewport = widget();

    QBoxLayout* pLayout  = (QBoxLayout*)pViewport->layout();

    if (pEvent->key() == Qt::Key_Space)
    {
        pLayout->addWidget(new CRootItem());

        qDebug() << "adding...";
    }

    if (pEvent->key() == Qt::Key_C)
    {
        if (!pLayout->isEmpty())
        {
            QLayoutItem* pItem = pLayout->itemAt(0);
            pLayout->removeItem(pItem);

            delete pItem->widget();
            delete pItem;

            qDebug() << "removing...";
        }
    }
}

Here is the image which shows child widgets overlapping after inserting a couple of new items: enter image description here

EDIT

Just solved my problem using dirty approach: subtraction and adding item fixed height from viewport's. Is there more fashion way to handle this problem?

Code:

void CMainWidget::keyPressEvent(QKeyEvent *pEvent)
{
    QWidget* pViewport = widget();

    QBoxLayout* pLayout  = (QBoxLayout*)pViewport->layout();

    if (pEvent->key() == Qt::Key_Space)
    {
        QWidget* pItem = new CRootItem();

        pLayout->addWidget(pItem);

        QSize Size = pViewport->size();

        Size.rheight() += pItem->height() + pLayout->spacing();

        pViewport->resize(Size);

        qDebug() << "adding...";
    }

    if (pEvent->key() == Qt::Key_C)
    {
        if (!pLayout->isEmpty())
        {
            QLayoutItem* pItem = pLayout->itemAt(0);
            pLayout->removeItem(pItem);

            QSize Size = pViewport->size();

            Size.rheight() -= pItem->widget()->height() + pLayout->spacing();

            pViewport->resize(Size);

            delete pItem->widget();
            delete pItem;

            qDebug() << "removing...";
        }
    }
}

Solution

  • Comment out your resizeEvent. You rarely need to set the explicit size or position of a widget unless it's a top level window. Let the layout do the work.