当前位置: 首页 > news >正文

Qt5 GUI 编程详解

个人博客:blogs.wurp.top

一、核心概念

1. 信号与槽(Signals & Slots)

  • 信号:事件触发的通知(如按钮点击clicked()
  • :响应信号的函数(普通成员函数或 lambda)
  • 连接方式
    QObject::connect(sender, &SenderClass::signal, receiver, &ReceiverClass::slot);
    // 示例:按钮点击关闭窗口
    QObject::connect(button, &QPushButton::clicked, window, &QWidget::close);
    

2. 窗口部件(Widgets)

  • 基础控件:QPushButton, QLabel, QLineEdit, QCheckBox
  • 容器控件:QMainWindow, QDialog, QTabWidget, QGroupBox

3. 布局管理器(Layouts)

  • 自动排列控件:QVBoxLayout(垂直)、QHBoxLayout(水平)、QGridLayout(网格)
  • 使用示例:
    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(button1);
    layout->addWidget(button2);
    window->setLayout(layout);
    

二、开发流程

1. 创建基本窗口

#include <QApplication>
#include <QMainWindow>
#include <QPushButton>int main(int argc, char *argv[]) {QApplication app(argc, argv);  // 每个应用必须的入口对象QMainWindow window;window.setWindowTitle("Qt5 Demo");window.resize(400, 300);QPushButton button("Click Me!", &window);button.setGeometry(50, 50, 100, 40);  // (x, y, width, height)window.show();return app.exec();  // 进入事件循环
}

2. 使用布局管理控件

// 在QMainWindow中添加中心部件
QWidget *centralWidget = new QWidget(&window);
window.setCentralWidget(centralWidget);QVBoxLayout *mainLayout = new QVBoxLayout(centralWidget);QLineEdit *edit = new QLineEdit;
QPushButton *btn = new QPushButton("Submit");mainLayout->addWidget(edit);
mainLayout->addWidget(btn);

3. 自定义槽函数

class MyWindow : public QMainWindow {Q_OBJECT  // 必须包含宏以支持信号槽
public:MyWindow() {QPushButton *btn = new QPushButton("Show Message", this);connect(btn, &QPushButton::clicked, this, &MyWindow::showMessage);}private slots:  // 槽函数声明void showMessage() {QMessageBox::information(this, "Info", "Button Clicked!");}
};

三、进阶功能

1. Qt Designer 设计 UI

  • 使用 .ui 文件可视化设计界面
  • 编译为 C++ 代码:uic input.ui -o ui_header.h
  • 在代码中加载:
    #include "ui_myform.h"
    class MyApp : public QWidget {Q_OBJECT
    public:MyApp() {ui.setupUi(this);  // 自动初始化界面}
    private:Ui::MyForm ui;  // 设计师生成的类
    };
    

2. 多窗口交互

// 主窗口打开子窗口
void MainWindow::openDialog() {QDialog dialog(this);dialog.exec();  // 模态显示
}

3. 绘图与动画

  • 绘图:重写 paintEvent() 使用 QPainter
  • 动画:QPropertyAnimation
    QPropertyAnimation *anim = new QPropertyAnimation(button, "geometry");
    anim->setDuration(1000);
    anim->setStartValue(QRect(0,0,100,30));
    anim->setEndValue(QRect(200,150,100,30));
    anim->start();
    

四、项目配置(qmake)

# Qt5 项目文件示例 (myapp.pro)
QT       += core gui widgets
TARGET    = MyApp
SOURCES  += main.cpp \mywindow.cpp
HEADERS  += mywindow.h

五、跨平台注意事项

1. 文件路径:使用QDirQFileInfo代替平台相关路径

2. 样式适配:使用 QStyleFactory 设置平台风格

QApplication::setStyle("Fusion");  // 统一跨平台风格

3. 高 DPI 支持

QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);  // 启用缩放

六、最佳实践

1. 内存管理:Qt 对象树自动释放(设置 parent 参数)

2. 线程安全

  • GUI 操作只能在主线程
  • 耗时操作使用 QThreadQtConcurrent

3. 国际化:使用 tr() 包裹字符串,配合 Qt Linguist 翻译

http://www.dtcms.com/a/339067.html

相关文章:

  • 【AI大模型的发展历史】从Transformer到2025年的多模态、推理与开源革命
  • mlir 类型
  • docker 数据卷、自定义镜像操作演示分享(第二期)
  • 【数据结构】堆和二叉树详解(下)
  • SpringAI——向量存储(vector store)
  • SpringClound——网关、服务保护和分布式事务
  • Redis-缓存-击穿-分布式锁
  • 使用ros2跑mid360的fastlio2算法详细教程
  • 【数据结构】用堆解决TOPK问题
  • 算法训练营day56 图论⑥ 108. 109.冗余连接系列
  • C++---为什么迭代器常用auto类型?
  • 强、软、弱、虚引用
  • 在 Qt C++ 中利用 OpenCV 实现视频处理技术详解
  • 尝试Claude Code的安装
  • 学习笔记分享——基于STM32的平衡车项目
  • Mac调试ios的safari浏览器打开的页面
  • 电子电气架构 --- 软件项目成本估算
  • 技术攻坚全链铸盾 锁定12月济南第26届食品农产品安全高峰论坛
  • 任务十二 我的页面及添加歌曲功能开发
  • Typescript入门-对象讲解
  • Python量化交易:结合爬虫与TA-Lib技术指标分析
  • Matplotlib数据可视化实战:Matplotlib子图布局与管理入门
  • Ansible 角色管理指南
  • Pandas数据处理与分析实战:Pandas数据处理与Matplotlib可视化入门
  • 0819 使用IP多路复用实现TCP并发服务器
  • Tomcat 的核心脚本catalina.sh 和 startup.sh的关系
  • 陪诊小程序系统开发:开启智慧就医新时代
  • CNN 在故障诊断中的应用:原理、案例与优势
  • BEV:隐式相机视角转换-----BEVFormer
  • 简单实现监听redis的Key过期事件