一杯清酒邀明月
天下本无事,庸人扰之而烦耳。
posts - 3121,comments - 209,views - 578万

最近在修改一个软件的过程中,把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 }
复制代码

 

posted on   一杯清酒邀明月  阅读(1977)  评论(0编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
历史上的今天:
2020-03-30 Qt QLineEdit 信号函数总结
2020-03-30 Qt 主窗口与子窗口之间传值
2020-03-30 Qt 如何使窗体初始最大化
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示