gpt4 book ai didi

c++ - 在 QBoxLayout 中添加新的子窗口小部件期间,Qt 视口(viewport)窗口小部件不会扩展

转载 作者:行者123 更新时间:2023-11-30 05:27:33 24 4
gpt4 key购买 nike

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

代码如下:

主窗口部件.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

主窗口部件.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

编辑

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

代码:

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。除非它是顶级窗口,否则您很少需要设置小部件的显式大小或位置。让布局完成工作。

关于c++ - 在 QBoxLayout 中添加新的子窗口小部件期间,Qt 视口(viewport)窗口小部件不会扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37227116/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com