这里写自定义目录标题
QT自定义网格布局
效果:
功能:新增项,和删除选中项,自动布局
主要问题:Qlist 删除指定项时,迭代器会自动向后移动,注意遍历问题
代码:
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
void flushQheckBoxNum();
private:
Ui::Widget *ui;
QGridLayout*m_gird;
QList<QCheckBox*> m_boxList;
int m_col = 4; // 4列
};
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
,m_gird(new QGridLayout())
{
ui->setupUi(this);
ui->widget->setLayout(m_gird);
for(int i = 0;i<10;i++){
m_boxList.append(new QCheckBox(QString::number(i+1)));
}
connect(ui->addBtn,&QPushButton::clicked,[=](){
int max = m_boxList.size();
m_boxList.append(new QCheckBox(QString::number(max+1)));
flushQheckBoxNum();
});
connect(ui->delBtn,&QPushButton::clicked,[=](){
for(int i = 0;i<m_boxList.size();){
if(m_boxList[i]->isChecked()){
delete m_boxList[i];
m_boxList.removeAt(i);
}else{
i++;
}
}
flushQheckBoxNum();
});
}
Widget::~Widget()
{
delete ui;
}
void Widget::flushQheckBoxNum()
{
int row = 0;
if(m_boxList.size()%m_col == 0){
row = m_boxList.size()/m_col;
}else{
row = m_boxList.size()/m_col+1;
}
for(int i = 0;i<row;i++){
for(int j = 0;j<m_col;j++){
if(i*m_col+j<m_boxList.size()){
m_gird->addWidget(m_boxList[i*m_col+j],i,j,1,1);
}
}
}
}