QGridLayout动态添加控件

1、动态生成

QGridLayout* lay = new QGridLayout();
int total = paras.size();
int colum = 4;
for(int i=0; i<total; i++){
  Zparam* pZparam = new Zparam(this);
  m_params.push_back(pZparam);
  lay->addWidget(pZparam, i/colum, i%colum);
}
this->setLayout(lay);

Zparam自定义QWidget子类;m_params用来存储动态生成的控件,便于回收

 

2、回收

delete this->layout();
for(int i=0; i<(int)m_params.size(); i++){
    delete m_params[i];
}
m_params.clear();

 

posted @ 2020-12-15 15:20  朱小勇  阅读(1416)  评论(1编辑  收藏  举报