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

 1 #include "mainwindow.h"
 2 #include "ui_mainwindow.h"
 3 
 4 MainWindow::MainWindow(QWidget *parent) :
 5     QMainWindow(parent),
 6     ui(new Ui::MainWindow)
 7 {
 8     ui->setupUi(this);
 9     m_gLayout=new QGridLayout;
10     ui->widget->setLayout(m_gLayout);
11     count=0;
12 }
13 
14 MainWindow::~MainWindow()
15 {
16     delete ui;
17 }
18 
19 void MainWindow::on_add_btn_clicked()
20 {
21     if(ui->lineEdit->text()!=""){
22         count=mtestForms.size();
23         testForm *mtestForm=new testForm();
24         mtestForms.append(mtestForm);
25         mtestForm->setEdit(ui->lineEdit->text());
26         connect(mtestForm,SIGNAL(mdel()),this,SLOT(mdel_btn_clicked()));
27         m_gLayout->addWidget(mtestForm);
28     }else{
29         QMessageBox::information(this,"warming","请输入类别");
30     }
31 }
32 
33 void MainWindow::mdel_btn_clicked()
34 {
35     testForm *tf = qobject_cast<testForm *>(sender()); //这一步能够判断你点击的是哪一个控件
36     m_gLayout->removeWidget(tf);
37     tf->deleteLater();
38 }

testfom.h

 1 #ifndef TESTFORM_H
 2 #define TESTFORM_H
 3 #include <QWidget>
 4 #include <QGroupBox>
 5 #include <QHBoxLayout>
 6 #include <QLabel>
 7 #include <QLineEdit>
 8 #include <QPushButton>
 9 #include <QTimer>
10 #include <QDebug>
11 class testForm:public QWidget
12 {
13     Q_OBJECT
14 public:
15     testForm();
16     QPushButton *m_btn_start;
17     QPushButton *m_btn_delete;
18     QLabel *m_label_Name;
19     QLabel *m_label_Count;
20     QLineEdit *m_lineEidt;
21     QHBoxLayout *m_hlayoutLabel;
22     QTimer *ask_COM_Timer;
23     void setEdit(QString type);
24     int count;
25 
26 private slots:
27     void del_btn_click();
28     void startTimer();
29     void printcount();
30 
31 signals:
32     void mdel();
33 };
34 
35 #endif // TESTFORM_H

testfom.cpp

 1 #include "testform.h"
 2 
 3 testForm::testForm()
 4 {
 5     m_btn_start=new QPushButton();
 6   connect(m_btn_start,SIGNAL(clicked()),this,SLOT(startTimer()));
 7     m_btn_start->setText("启动");
 8     m_btn_delete=new QPushButton();
 9   connect(m_btn_delete,SIGNAL(clicked()),this,SLOT(del_btn_click()));
10     m_btn_delete->setText("删除");
11     ask_COM_Timer=new QTimer(this);
12     ask_COM_Timer->setInterval(1000);
13   connect(ask_COM_Timer,SIGNAL(timeout()),this,SLOT(printcount()));
14     m_label_Name=new QLabel();
15     m_label_Count=new QLabel();
16     m_lineEidt=new QLineEdit();
17     m_hlayoutLabel=new QHBoxLayout;
18     m_hlayoutLabel->addWidget(m_btn_start,0,nullptr);
19     m_hlayoutLabel->addWidget(m_lineEidt,1,nullptr);
20     m_hlayoutLabel->addWidget(m_label_Name,2,nullptr);
21     m_hlayoutLabel->addWidget(m_label_Count,3,nullptr);
22     m_hlayoutLabel->addStretch(4);
23     m_hlayoutLabel->addWidget(m_btn_delete,5,nullptr);
24 
25     this->setLayout(m_hlayoutLabel);
26     QSizePolicy policy = this->sizePolicy();
27     policy.setHorizontalPolicy(QSizePolicy::Preferred);
28     policy.setVerticalPolicy(QSizePolicy::Fixed);
29     this->setSizePolicy(policy);
30 }
31 
32 void testForm::del_btn_click()
33 {
34     emit mdel();
35 }
36 
37 void testForm::setEdit(QString type)
38 {
39     m_lineEidt->setText(type);
40     m_label_Name->setText(type);
41 }
42 
43 void testForm::startTimer()
44 {
45     count=0;
46     if(m_btn_start->text()=="启动"){
47         ask_COM_Timer->start();
48         m_btn_start->setText("停止");
49     }else if(m_btn_start->text()=="停止"){
50         ask_COM_Timer->stop();
51         m_btn_start->setText("启动");
52     }
53 }
54 
55 void testForm::printcount(){
56     qDebug()<<m_lineEidt->text()+"  "+QString::number(count);
57     m_label_Count->setText(QString::number(count));
58     count++;
59 }

 

原文地址:https://www.cnblogs.com/ybqjymy/p/14596751.html