最近在修改一个软件的过程中,把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++;
}
工程文件下载:动态增减控件代码