QT中UI实现小功能的步骤

1、新建一个目录

要勾选Generate form,生成一个.ui文件

把Shadow build的勾选取消,避免产生两个文件夹!

2、具体步骤

1.在UI界面中拖拽需要的组件对象

2.修改组件对象的名称

3.保存并运行,根据运行结果适当调整界面布局

4.业务处理 选中组件对象,右键 转到槽 ,选择信号, 确定

5.在生成的槽函数中 增加业务处理代码即可

6.验证!

3、使用UI界面实现一个简易的运算器

1、UI界面设计如下

2、demo.h文件如下

复制代码
#ifndef DEMO1_H
#define DEMO1_H

#include <QWidget>

QT_BEGIN_NAMESPACE
namespace Ui { class demo1; }
QT_END_NAMESPACE

class demo1 : public QWidget
{
    Q_OBJECT

public:
    demo1(QWidget *parent = nullptr);
    ~demo1();
    void op_clicked();
    void clear_clicked();

private slots:
    void on_btn_sum_clicked();

    void on_btn_clear_clicked();

private:
    Ui::demo1 *ui;
};
#endif // DEMO1_H

3、 demo.cpp文件如下

复制代码
#include "demo1.h"
#include "ui_demo1.h"
#include <QString>
#include <QMessageBox>

demo1::demo1(QWidget *parent): QWidget(parent), ui(new Ui::demo1)
{
    ui->setupUi(this);
}

demo1::~demo1()
{
    delete ui;
}

void demo1::on_btn_sum_clicked()
{
    double sop1 = this->ui->op1->text().toDouble();
    double sop2 = this->ui->op2->text().toDouble();

    if(this->ui->comboBox->currentText()=="+")
    {
        this->ui->res->setText(QString::number((sop1 + sop2),'f',2));
    }
    else if(this->ui->comboBox->currentText()== "-")
    {
        this->ui->res->setText(QString::number((sop1 - sop2),'f',2));
    }
    else if(this->ui->comboBox->currentText()== "*")
    {
        this->ui->res->setText(QString::number((sop1 * sop2),'f',2));
    }
    else if(this->ui->comboBox->currentText()== "/")
    {
        if(sop2 == 0)
        {
            this->ui->op2->clear();
            QMessageBox::warning(this,"警告","除数不能为0!!!");
        }
        this->ui->res->setText(QString::number((sop1 / sop2),'f',2));
    }
}


void demo1::on_btn_clear_clicked()
{
    this->ui->op1->clear();
    this->ui->op2->clear();
    this->ui->res->clear();
}

4、具体效果

相关推荐
西岸行者3 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
修炼前端秘籍的小帅3 天前
Stitch——Google热门的免费AI UI设计工具
前端·人工智能·ui
王码码20353 天前
Flutter for OpenHarmony:socket_io_client 实时通信的事实标准(Node.js 后端的最佳拍档) 深度解析与鸿蒙适配指南
android·flutter·ui·华为·node.js·harmonyos
悠哉悠哉愿意3 天前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
别催小唐敲代码3 天前
嵌入式学习路线
学习
毛小茛3 天前
计算机系统概论——校验码
学习
babe小鑫3 天前
大专经济信息管理专业学习数据分析的必要性
学习·数据挖掘·数据分析
winfreedoms3 天前
ROS2知识大白话
笔记·学习·ros2
在这habit之下3 天前
Linux Virtual Server(LVS)学习总结
linux·学习·lvs
我想我不够好。3 天前
2026.2.25监控学习
学习