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

工程文件下載:動態增減控件代碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章