Qt layout下遍历删除子控件

场景1:layout下的控件删除

背景:工作需要,动态的增加一组组件和删除一组组件。
如下示意,点击delete,将QVBoxLayout下的四个button进行删除。
布局代码如下:
Qt layout下遍历删除子控件_第1张图片

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QPushButton* btn = new QPushButton("Delete",this);
    connect(btn,&QPushButton::clicked,this,&MainWindow::traverseDelete);
    //产生窗口
    initWindow();

    //遍历删除
    //traverseDelete();

    QVBoxLayout* mainLayout = new QVBoxLayout;
    mainLayout->addLayout(vLayout);
    mainLayout->addSpacing(50);
    mainLayout->addWidget(btn);
    QWidget* widget = new QWidget(this);
    widget->setLayout(mainLayout);
    setCentralWidget(widget);
    setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
    resize(sizeHint());
    setWindowTitle("layout");
}

    void MainWindow::initWindow()
    {
        QPushButton* btn1 = new QPushButton("Button1");
        QPushButton* btn2 = new QPushButton("Button2");
        QPushButton* btn3 = new QPushButton("Button3");
        QPushButton* btn4 = new QPushButton("Button4");
        vLayout = new QVBoxLayout;
        vLayout->addWidget(btn1);
        vLayout->addWidget(btn2);
        vLayout->addWidget(btn3);
        vLayout->addWidget(btn4);
    }
void MainWindow::initWindow()
{
    QPushButton* btn1 = new QPushButton("Button1");
    QPushButton* btn2 = new QPushButton("Button2");
    QPushButton* btn3 = new QPushButton("Button3");
    QPushButton* btn4 = new QPushButton("Button4");
    vLayout = new QVBoxLayout;
    vLayout->addWidget(btn1);
    vLayout->addWidget(btn2);
    vLayout->addWidget(btn3);
    vLayout->addWidget(btn4);
}

void MainWindow::traverseDelete()
{
    QLayoutItem* child;
    while((child = vLayout->takeAt(0)) != 0){
        //必须将parent置为nullpptr,否则不消失
        child->widget()->setParent(NULL);
        delete child;
    }
}

Qt layout下遍历删除子控件_第2张图片

场景2:layout下的layout下的控件遍历删除

Qt layout下遍历删除子控件_第3张图片

布局代码如下:

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QPushButton* btn = new QPushButton("Delete",this);
    //connect(btn,&QPushButton::clicked,this,&MainWindow::traverseDelete);
    connect(btn,&QPushButton::clicked,this,&MainWindow::traverseDelete1);
    //产生窗口
    //initWindow1();
    initWindow2();

    //遍历删除
    //traverseDelete();

    QVBoxLayout* mainLayout = new QVBoxLayout;
    mainLayout->addLayout(hLayout);
    mainLayout->addSpacing(50);
    mainLayout->addWidget(btn);
    QWidget* widget = new QWidget(this);
    widget->setLayout(mainLayout);
    setCentralWidget(widget);
    setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
    resize(sizeHint());
    setWindowTitle("layout");
}
void MainWindow::initWindow2()
    {
        QPushButton* btn1 = new QPushButton("Button1");
        QPushButton* btn2 = new QPushButton("Button2");
        QPushButton* btn3 = new QPushButton("Button3");
        QPushButton* btn4 = new QPushButton("Button4");

        QPushButton* btn5 = new QPushButton("Button5");
        QPushButton* btn6 = new QPushButton("Button6");
        QPushButton* btn7 = new QPushButton("Button7");
        QPushButton* btn8 = new QPushButton("Button8");
        hLayout = new QHBoxLayout;
        QVBoxLayout* vLayout1 = new QVBoxLayout;
        QVBoxLayout* vLayout2 = new QVBoxLayout;
        vLayout1->addWidget(btn1);
        vLayout1->addWidget(btn2);
        vLayout1->addWidget(btn3);
        vLayout1->addWidget(btn4);

        vLayout2->addWidget(btn5);
        vLayout2->addWidget(btn6);
        vLayout2->addWidget(btn7);
        vLayout2->addWidget(btn8);
        hLayout->addLayout(vLayout1);
        hLayout->addLayout(vLayout2);
    }
void MainWindow::traverseDelete1()
{
    QLayoutItem* layoutChild;
    QLayoutItem* child;
    while((layoutChild=hLayout->takeAt(0)) !=0){
        while((child=layoutChild->layout()->takeAt(0))!=0){
            child->widget()->setParent(NULL);
            delete child;
        }
    }

}

场景3:layout下的layout下的gridLayout组件的遍历操作

Qt layout下遍历删除子控件_第4张图片

void MainWindow::on_pushButton_4_clicked()
{
    qDebug()<<"count:"<<ui->verticalLayout_2->count()<<endl;
    QLayoutItem* layoutItem_=nullptr;
    QGridLayout* gridLayoutItem=nullptr;
    QLayoutItem* widgetItem=nullptr;
    QPushButton* btn=nullptr;
    QString str;
    for(int i=0;i<ui->verticalLayout_2->count();++i){
        layoutItem_ = ui->verticalLayout_2->itemAt(i);
        gridLayoutItem=qobject_cast<QGridLayout *>(layoutItem_->layout());
        if(!gridLayoutItem){
            qDebug()<<"nullptr..."<<endl;
            return;
        }
        for(int row=0;row<gridLayoutItem->rowCount();++row){
            for(int col=0;col<gridLayoutItem->columnCount();++col){
                widgetItem=gridLayoutItem->itemAtPosition(row,col);
                if(!widgetItem){
                    qDebug()<<"111111..."<<endl;
                    return;
                }
                btn =qobject_cast<QPushButton *>(widgetItem->widget());
                str=QString("%1,%2").arg(row).arg(col);
                btn->setText(str);
            }
        }
    }

}

Qt layout下遍历删除子控件_第5张图片

Qt layout下遍历删除子控件_第6张图片
Qt layout下遍历删除子控件_第7张图片
Qt layout下遍历删除子控件_第8张图片
Qt layout下遍历删除子控件_第9张图片

你可能感兴趣的:(QT,C++,qt)