Qt 동적으로 컨트롤 추가 및 지정 컨트롤 삭제

4766 단어 QT
최근에 한 소프트웨어를 수정하는 과정에서 Qt 컨트롤을 다시 썼다. 다시 쓴 후에 레이아웃이 더욱 간단하고 합리적이어서 버그가 발생할 확률이 낮아졌다.

실현 가능한 기능


1. 흩어진 컨트롤러를 하나의 모듈로 포장하여 전체 모듈을 추가한다.2. 각 모듈의 단독 삭제를 실현한다.3. 수치, 기능의 상호작용 기능을 실현한다.버튼 등.

코드 내용


말을 많이 하지 말고 바로 코드를 붙여라.손수 쓴 것이기 때문에 주석이 많지 않으니 스스로 깨닫고 공사 서류를 문말에 놓아라.mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H


#include 
#include 
#include 
#include 


namespace Ui {
class MainWindow;
}


class MainWindow : public QMainWindow
{
    Q_OBJECT


public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    QGridLayout *m_gLayout;
    QListmtestForms;
    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(sender()); //                 
    m_gLayout->removeWidget(tf);
    tf->deleteLater();
}

testfom.h
#ifndef TESTFORM_H
#define TESTFORM_H
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
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()<text()+"  "+QString::number(count);
    m_label_Count->setText(QString::number(count));
    count++;
}

프로젝트 파일 다운로드:동적 증감 컨트롤 코드

좋은 웹페이지 즐겨찾기