我正在尝试删除两个工具按钮并添加其他小部件,并在显示或隐藏窗口时使用偶数过滤器添加hboxlayout。 问题是当我删除水平隔离垫时,两个按钮成功连接在一起,但是当我添加一个小部件时,工具按钮和小部件之间有一个空间,如图所示。我该如何删除该空间?
这是初始化
setWindowTitle(tr("Points of Interest"));
m_splitter->setContentsMargins(0, 0, 0, 3);
m_gridLayout = new QGridLayout(this);
m_gridLayout->addWidget(m_splitter, 0, 0, 1, 1);
m_hBoxLayout = new QHBoxLayout(this);
m_gridLayout->addLayout(m_hBoxLayout, 1, 0, 1, 1);
m_hBoxLayout->addWidget(m_myPoiButton);
m_hBoxLayout->addStretch();
m_hBoxLayout->addWidget(m_myPoiFilterButton);
m_gridLayout->setVerticalSpacing(0);
这是代码和图像:
if (event->type() == QEvent::Show)
{
if (obj == m_poiItemWidget)
{
bool visibleName = false, visibleHeight = false, visibleRange = false;
visibleName = m_namePoiFilter.isVisible();
visibleHeight = m_widgetHeight.isVisible();
visibleRange = m_widgetRange.isVisible();
m_namePoiFilter.setVisible(visibleName);
m_widgetHeight.setVisible(visibleHeight);
m_widgetRange.setVisible(visibleRange);
m_hBoxLayout->addWidget(m_myPoiButton);
for (int i = 0; i < m_hBoxLayout->count(); ++i)
{
QSpacerItem *spacer = m_hBoxLayout->itemAt(i)->spacerItem();
if (spacer)
{
delete m_hBoxLayout->takeAt(i);
break;
}
}
m_hBoxLayout->addWidget(m_myPoiFilterButton);
m_hBoxLayout->addStretch();
m_hBoxLayout->addWidget(&m_namePoiFilter);
m_hBoxLayout->addWidget(&m_widgetHeight);
m_hBoxLayout->addWidget(&m_widgetRange);
}
}
if (event->type() == QEvent::Hide)
{
if (obj == m_poiItemWidget)
{
bool visibleName = false, visibleHeight = false, visibleRange = false;
visibleName = m_namePoiFilter.isVisible();
visibleHeight = m_widgetHeight.isVisible();
visibleRange = m_widgetRange.isVisible();
m_namePoiFilter.setVisible(visibleName);
m_widgetHeight.setVisible(visibleHeight);
m_widgetRange.setVisible(visibleRange);
for (int i = 0; i < m_hBoxLayout->count(); ++i)
{
QSpacerItem *spacer = m_hBoxLayout->itemAt(i)->spacerItem();
if (spacer)
{
delete m_hBoxLayout->takeAt(i);
break;
}
}
m_hBoxLayout->addWidget(m_myPoiButton);
m_hBoxLayout->addStretch();
m_hBoxLayout->addWidget(m_myPoiFilterButton);
m_hBoxLayout->addWidget(&m_namePoiFilter);
m_hBoxLayout->addWidget(&m_widgetHeight);
m_hBoxLayout->addWidget(&m_widgetRange);
}
}
答案 0 :(得分:0)
在初始化中尝试此操作
m_gridLayout->setContentsMargins(0,0,0,0);
m_hBoxLayout->setContentsMargins(0,0,0,0);