Qt layout下遍历删除子控件

12 篇文章 1 订阅

场景1:layout下的控件删除

背景:工作需要,动态的增加一组组件和删除一组组件。
如下示意,点击delete,将QVBoxLayout下的四个button进行删除。
布局代码如下:
在这里插入图片描述

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;
    }
}

在这里插入图片描述

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

在这里插入图片描述

布局代码如下:

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组件的遍历操作

在这里插入图片描述

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);
            }
        }
    }

}

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alex1_Code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值