Qt动态布局

时间:2024-11-27 11:07:19

QVBoxLayout *m_pvLayout = NULL;
QWidget *m_pWidgetPlay = NULL;

m_pvLayout = new QVBoxLayout(this);
m_pvLayout->setSpacing(0);
m_pvLayout->setContentsMargins(0, 0, 0, 0);
this->setLayout(m_pvLayout);

//布局管理器添加控件

m_pWidgetPlay = new QWidget();
m_pWidgetPlay->setStyleSheet("QWidget{background:rgba(0, 200, 44, 1);image:none;}");
m_pvLayout->addWidget(m_pWidgetPlay);

//布局管理器移除控件,并将控件释放掉

if (m_pWidgetPlay != nullptr)
{
  m_pvLayout->removeWidget(m_pWidgetPlay);

  delete m_pWidgetPlay;
  m_pWidgetPlay = nullptr;
}