Qt 在程序中动态添加 删除 View 布局 或控件

原因:

有时我们需要在程序中动态添加布局或控件等,下面用程序来展示一下相应的方法:

效果:

 点击删除:

 删除所有控件:

实现如下:

testlayout.h

#ifndef TESTLAYOUT_H
#define TESTLAYOUT_H

#include <QWidget>
#include <QPushButton>
#include <QGroupBox>
#include <QVBoxLayout>

class TestLayout : public QWidget
{
    Q_OBJECT
public:
    explicit TestLayout(QWidget *parent = nullptr);

    void removeAllWidget();
signals:

public slots:


private:
    QWidget* m_Parent;

    QGroupBox* buttonGroup;
    QVBoxLayout* widgetLayout;
    QList<QPushButton*> m_TrackerButtonList;
};

#endif // TESTLAYOUT_H

testlayout.cpp

#include "testlayout.h"

TestLayout::TestLayout(QWidget *parent) : QWidget(parent),widgetLayout(nullptr)
{
    m_Parent = parent;

    for (int i = 0; i < 7; i++)
    {
        QPushButton *button = new QPushButton("");
        button->setFixedSize(QSize(60 , 20 ));
        button->setStyleSheet(QString("border: 1px solid #323232; \
                                        border-radius:0px;  \
                                        padding:0px 0px;  \
                                        background-color:#126215;"));
        this->m_TrackerButtonList.push_back(button);
    }

    QHBoxLayout* trackerLayout = new QHBoxLayout();
    trackerLayout->setObjectName("trackerButtonLayout");
    trackerLayout->setContentsMargins(5, 5, 5, 5);
    trackerLayout->addStretch();


    for (size_t i = 0; i < this->m_TrackerButtonList.size(); i++)
    {
        trackerLayout->addWidget(this->m_TrackerButtonList[i]);
        trackerLayout->addStretch();
    }


    buttonGroup = new QGroupBox();
    buttonGroup->setLayout(trackerLayout);
    buttonGroup->setStyleSheet(QString("border: 1px solid #323232; \
                                    border-radius:0px;  \
                                    padding:0px 0px;  \
                                    background-color:#121315;"));


    widgetLayout = new QVBoxLayout(m_Parent);
    widgetLayout->addWidget(buttonGroup);

    this->setLayout(widgetLayout);
}

void TestLayout::removeAllWidget()
{

    QLayout* _layout = this->layout();
    if (_layout)
    {
        int xdd = _layout->count();
        for (int i = 0; i < _layout->count(); ++i)
        {
            if (QLayoutItem* _item = _layout->itemAt(i))
            {
                if (QWidget* _itemWidget = _item->widget())
                {
                    _itemWidget->setParent(NULL);
                    _layout->removeWidget(_itemWidget);
                }

                else
                    delete _layout->takeAt(i);
            }
        }
    }

}


mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

     mTestLayout = new TestLayout(this);
     ui->verticalLayout->insertWidget(0,mTestLayout);
    // ui->verticalLayout->addWidget(mTestLayout);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{

   mTestLayout->removeAllWidget();
   // mTestLayout->show();
    //ui->verticalLayout->removeWidget(mTestLayout);
  //  this->update();
}

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

恋恋西风

up up up

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

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

打赏作者

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

抵扣说明:

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

余额充值