在 QBoxLayout 中添加新的子窗口小部件期间,Qt 视口窗口小部件不会扩展

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

我想创建一个可滚动的小部件,其中包含由 QBoxLayout 管理的子小部件集合,并且必须可以轻松地从该集合中添加和删除小部件。但是当我向它添加子窗口小部件时,视口窗口小部件并没有扩大其大小(保持初始大小),而是子窗口小部件重叠在自己身上。我不知道如何解决这个问题。

代码如下:

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...";
        }
    }
}

这是显示插入几个新项目后重叠的子窗口小部件的图像:

编辑

刚刚使用肮脏的方法解决了我的问题:从视口中减去和添加固定高度的项目。有更多时尚方法来处理这个问题吗?

代码:

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...";
        }
    }
}

注释掉你的resizeEvent。您很少需要设置小部件的显式大小或位置,除非它是顶级 window。让布局完成工作。