前言
这里只是简单介绍QT以及使用QT写一个简单的计算器
一、Qt 框架简介
Qt 是一个跨平台的 C++ 应用程序开发框架,由挪威公司 Trolltech(现为 The Qt Company)开发。它不仅包含丰富的 GUI 组件库,还提供网络、数据库、多媒体等全栈解决方案。
核心特点:
- 跨平台支持:一次编写,编译部署到 Windows/macOS/Linux/Android/iOS
- 双授权模式:商业授权(闭源)和 LGPL/GPL 开源授权
- 多语言绑定:支持 Python(PyQt/PySide)、Rust 等语言
- 现代化架构:信号槽机制、元对象系统(MOC)
- 丰富模块:
- Qt Widgets:传统桌面UI组件
- Qt Quick:声明式UI(QML)用于现代界面
- Qt Network:网络通信
- Qt SQL:数据库集成
应用场景:
- 工业控制软件(如汽车ECU)
- 医疗设备界面
- 嵌入式系统
- 跨平台桌面应用(如 WPS、VirtualBox)
二、Qt 安装指南
1. Windows/macOS 安装
- 访问 Qt 官网
- 下载 Qt Online Installer
- 运行安装程序:
- 选择组件:
- Qt Creator(IDE)
- Qt 6.x(最新稳定版)
- MinGW/MSVC(Windows编译工具链)
- Qt Charts(可选,图表模块)
- 设置安装路径(建议C盘外)
2. Linux 安装(Ubuntu/Debian)
1 2 3 4
| sudo apt update sudo apt install qtcreator qt6-base-dev qt6-tools-dev
qmake --version
|
3. 环境验证
- 启动 Qt Creator
- 创建示例项目:文件 → 新建项目 → Application → Qt Widgets Application
- 编译运行(Ctrl+R)显示空白窗口即成功
三、计算器开发实战
1. 创建项目
- 文件 → 新建项目 → Qt Widgets Application
- 命名:
SimpleCalculator
- 选择基类:QWidget(取消创建UI文件)
2. 界面设计(代码实现)
修改 mainwindow.cpp:
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 32 33 34 35 36
| #include "mainwindow.h" #include <QGridLayout> #include <QPushButton> #include <QLineEdit>
MainWindow::MainWindow(QWidget *parent) : QWidget(parent) { display = new QLineEdit(this); display->setReadOnly(true); display->setAlignment(Qt::AlignRight); display->setMaxLength(15);
const QString btnTexts[16] = { "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+" };
QGridLayout *grid = new QGridLayout; grid->addWidget(display, 0, 0, 1, 4);
for (int i = 0; i < 16; ++i) { QPushButton *btn = new QPushButton(btnTexts[i]); connect(btn, &QPushButton::clicked, this, &MainWindow::onButtonClicked); grid->addWidget(btn, 1 + i/4, i%4); }
setLayout(grid); setWindowTitle("简易计算器"); }
|
3. 计算逻辑实现
在 mainwindow.h 添加:
1 2 3 4 5 6 7 8
| private slots: void onButtonClicked(); private: QLineEdit *display; QString currentInput; double result; char lastOperator;
|
在 mainwindow.cpp 添加:
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 32 33 34 35
| void MainWindow::onButtonClicked() { QPushButton *clickedButton = qobject_cast<QPushButton*>(sender()); QString value = clickedButton->text(); if (value >= "0" && value <= "9" || value == ".") { currentInput += value; display->setText(currentInput); } else if (value == "=") { calculateResult(); } else { if (!currentInput.isEmpty()) { calculateResult(); } lastOperator = value.at(0).toLatin1(); result = currentInput.toDouble(); currentInput.clear(); } }
void MainWindow::calculateResult() { double inputNum = currentInput.toDouble(); switch (lastOperator) { case '+': result += inputNum; break; case '-': result -= inputNum; break; case '*': result *= inputNum; break; case '/': if (inputNum != 0) result /= inputNum; else display->setText("Error"); break; } currentInput = QString::number(result); display->setText(currentInput); }
|
4. 运行效果
1 2 3 4 5
| [ 显示区域 ] [7][8][9][/] [4][5][6][*] [1][2][3][-] [0][.][=][+]
|
四、项目编译与部署
1. 编译运行
- 点击左下角 ▶ 按钮(或 Ctrl+R)
- 自动生成构建目录并启动程序
2. 发布应用(Windows示例)
- 构建模式选择 Release
- 编译后进入
build-release 目录
- 使用 windeployqt 打包:
1
| windeployqt SimpleCalculator.exe
|
- 将生成的文件夹压缩分发
五、总结
Qt 框架核心优势:
✅ 真正的跨平台:同一代码库支持桌面/移动/嵌入式
✅ 高性能渲染:OpenGL/Vulkan加速的现代图形栈
✅ 生产力工具:Qt Designer可视化布局 + QML热重载
✅ 企业级支持:商业项目可购买官方技术支持
计算器项目关键技术点:
- 信号槽机制:实现按钮点击与逻辑的解耦
- 布局管理:QGridLayout自动排列控件
- 类型转换:QString与数值类型的转换
- 面向对象设计:利用C++特性构建组件
学习路径建议:
- 掌握基础Widgets组件(QPushButton/QLabel/QLineEdit)
- 理解布局管理器(QVBoxLayout/QHBoxLayout/QGridLayout)
- 精通信号槽通信机制
- 学习Qt Creator调试技巧
- 进阶探索QML现代UI开发
版权声明: 此文章版权归曦曦所有,如有转载,请注明来自原作者