Qt 动态添加控件,并删除指定控件

最近在修改一个软件的过程中,把Qt控件进行了重写,重写之后,布局更加简单、合理,如此使得出现bug的概率降低。

实现的功能

1、将零散的小控件打包成一个模块,进行整模块的添加。
2、实现每个模块的单独删除。
3、实现数值、功能的交互功能。如按钮等。

代码内容

话不多说,直接上代码。因为是顺手写的,注释不多,自己领会吧,工程文件放在文末。
mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H


#include <QMainWindow>
#include <testform.h>
#include <QDebug>
#include <QMessageBox>


namespace Ui {
class MainWindow;
}


class MainWindow : public QMainWindow
{
    Q_OBJECT


public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    QGridLayout *m_gLayout;
    QList<testForm *>mtestForms;
    int count;
    void delete_Layout();


private slots:
    void on_add_btn_clicked();


    void mdel_btn_clicked();


private:
    Ui::MainWindow *ui;
signals:
    void add_info(int num);
};


#endif // MAINWINDOW_H

mainwindow.cpp

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


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    m_gLayout=new QGridLayout;
    ui->widget->setLayout(m_gLayout);
    count=0;
}


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

void MainWindow::on_add_btn_clicked()
{
    if(ui->lineEdit->text()!=""){
        count=mtestForms.size();
        testForm *mtestForm=new testForm();
        mtestForms.append(mtestForm);
        mtestForm->setEdit(ui->lineEdit->text());
        connect(mtestForm,SIGNAL(mdel()),this,SLOT(mdel_btn_clicked()));
        m_gLayout->addWidget(mtestForm);
    }else{
        QMessageBox::information(this,"warming","请输入类别");
    }
}


void MainWindow::mdel_btn_clicked()
{
    testForm *tf = qobject_cast<testForm *>(sender()); //这一步能够判断你点击的是哪一个控件
    m_gLayout->removeWidget(tf);
    tf->deleteLater();
}

testfom.h

#ifndef TESTFORM_H
#define TESTFORM_H
#include <QWidget>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QTimer>
#include <QDebug>
class testForm:public QWidget
{
    Q_OBJECT
public:
    testForm();


    QPushButton *m_btn_start;


    QPushButton *m_btn_delete;


    QLabel *m_label_Name;


    QLabel *m_label_Count;


    QLineEdit *m_lineEidt;


    QHBoxLayout *m_hlayoutLabel;


    QTimer *ask_COM_Timer;


    void setEdit(QString type);


    int count;
private slots:
    void del_btn_click();


    void startTimer();


    void printcount();
signals:
    void mdel();
};


#endif // TESTFORM_H

testfom.cpp

#include "testform.h"








testForm::testForm()
{
    m_btn_start=new QPushButton();
    connect(m_btn_start,SIGNAL(clicked()),this,SLOT(startTimer()));
    m_btn_start->setText("启动");


    m_btn_delete=new QPushButton();
    connect(m_btn_delete,SIGNAL(clicked()),this,SLOT(del_btn_click()));
    m_btn_delete->setText("删除");


    ask_COM_Timer=new QTimer(this);
    ask_COM_Timer->setInterval(1000);
    connect(ask_COM_Timer,SIGNAL(timeout()),this,SLOT(printcount()));


    m_label_Name=new QLabel();
    m_label_Count=new QLabel();
    m_lineEidt=new QLineEdit();
    m_hlayoutLabel=new QHBoxLayout;


    m_hlayoutLabel->addWidget(m_btn_start,0,nullptr);
    m_hlayoutLabel->addWidget(m_lineEidt,1,nullptr);
    m_hlayoutLabel->addWidget(m_label_Name,2,nullptr);
    m_hlayoutLabel->addWidget(m_label_Count,3,nullptr);
    m_hlayoutLabel->addStretch(4);
    m_hlayoutLabel->addWidget(m_btn_delete,5,nullptr);


    this->setLayout(m_hlayoutLabel);
    QSizePolicy policy = this->sizePolicy();
    policy.setHorizontalPolicy(QSizePolicy::Preferred);
    policy.setVerticalPolicy(QSizePolicy::Fixed);
    this->setSizePolicy(policy);
}


void testForm::del_btn_click()
{
    emit mdel();
}
void testForm::setEdit(QString type)
{
    m_lineEidt->setText(type);
    m_label_Name->setText(type);
}
void testForm::startTimer()
{
    count=0;
    if(m_btn_start->text()=="启动"){
        ask_COM_Timer->start();
        m_btn_start->setText("停止");
    }else if(m_btn_start->text()=="停止"){
        ask_COM_Timer->stop();
        m_btn_start->setText("启动");
    }


}
void testForm::printcount(){
    qDebug()<<m_lineEidt->text()+"  "+QString::number(count);
    m_label_Count->setText(QString::number(count));
    count++;
}

工程文件下载:动态增减控件代码

  • 8
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

鱼月半

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

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

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

打赏作者

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

抵扣说明:

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

余额充值