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

相关推荐
Elias不吃糖4 小时前
Java Lambda 表达式
java·开发语言·学习
梨子串桃子_4 小时前
推荐系统学习笔记 | PyTorch学习笔记
pytorch·笔记·python·学习·算法
weixin_402486344 小时前
在adobe illustrator 上写latex code 显示数学公式 安装 LaTeX2Illustrator
ui·adobe·illustrator
jjjxxxhhh1234 小时前
spdlog介绍使用
学习
曾浩轩6 小时前
图灵完备Turing Complete 3
学习
天天睡大觉6 小时前
Python学习11
网络·python·学习
laplace01236 小时前
# 第六章 agent框架开发实践 - 学习笔记
人工智能·笔记·学习·语言模型·agent
坚持不懈的大白7 小时前
Leetcode学习笔记
笔记·学习·leetcode
SWAGGY..7 小时前
数据结构学习篇(10)--- 二叉树基础oj练习
数据结构·学习
QiZhang | UESTC8 小时前
学习日记day58
学习