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

时间:2016-05-14 13:14:56

标签: c++ qt widget qlayout

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

以下是代码:

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

这是一个图像,显示在插入几个新项目后,子窗口小部件重叠: enter image description here

编辑

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

代码:

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

1 个答案:

答案 0 :(得分:1)

评论您的resizeEvent。您很少需要设置窗口小部件的显式大小或位置,除非它是顶级窗口。让布局完成工作。