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、具体效果

相关推荐
星辰徐哥4 小时前
Spring Boot 数据导入导出与报表生成
spring boot·后端·ui
数智工坊5 小时前
机器人运动控制:采样、优化与学习三大流派深度对比与实战
android·学习·机器人
ZC跨境爬虫5 小时前
跟着 MDN 学JavaScript day_7:数学运算与逻辑判断实战测试
开发语言·前端·javascript·学习·ecmascript
MartinYeung58 小时前
[论文学习]隐私保护联邦特徵选择与差分隐私的的工程实践框架
学习
qeen878 小时前
【C++】类与对象之类的默认成员函数(二)
android·c语言·开发语言·c++·笔记·学习
Flandern11119 小时前
Pull Requests(PR)
学习·github·pr
nashane10 小时前
HarmonyOS 6学习:JsCrash“闪退”法医指南——从FaultLog堆栈还原崩溃现场的终极手册
学习·华为·harmonyos
for_ever_love__10 小时前
UI学习:UICollectionView瀑布流
学习·ui·ios·objective-c·cocoa
AOwhisky10 小时前
MySQL 学习笔记(第六期):MySQL 备份与恢复
运维·数据库·笔记·学习·mysql·云计算
_李小白10 小时前
【android opencv学习笔记】Day 32:直线检测之霍夫变换
android·opencv·学习