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

QT学习笔记3

一、2D绘图系统(QPainter)
1. 核心机制

QPainter工作流程

cpp

void Widget::paintEvent(QPaintEvent*) {
    QPainter painter(this); // 绑定到当前控件
    painter.setRenderHint(QPainter::Antialiasing); // 抗锯齿
    painter.drawLine(0,0,100,100); // 绘制操作
}
  • 绘图设备:QPixmap(图像缓冲)、QImage(像素级操作)、QWidget(控件绘图)
2. 高级绘图技术

路径操作

(QPainterPath):

cpp

QPainterPath path;
path.moveTo(20, 20);
path.lineTo(100, 20);
path.quadTo(150, 50, 100, 100); // 贝塞尔曲线
painter.drawPath(path);

复合变换

(缩放+旋转):

cpp

painter.save(); // 保存当前状态
painter.translate(50,50); // 移动坐标系
painter.rotate(45); // 旋转
painter.scale(2, 2); // 放大
painter.drawRect(0,0,10,10); // 应用所有变换后绘制
painter.restore(); // 恢复原始坐标系
3. 图像处理技巧

双缓冲技术

cpp

QPixmap buffer(size()); 
buffer.fill(Qt::transparent);
QPainter bufferPainter(&buffer);
// 在buffer上绘制复杂图形
painter.drawPixmap(0, 0, buffer); // 一次性绘制到屏幕

图像合成模式

cpp

painter.setCompositionMode(QPainter::CompositionMode_Overlay); // 叠加模式
4. 性能优化
  • 预渲染缓存:将静态内容绘制到QPixmap,避免重复计算
  • 局部更新:使用update(QRect)指定脏矩形区域

二、图形视图框架(Graphics View)
1. 体系结构详解

场景(QGraphicsScene)

cpp

scene = new QGraphicsScene(this);
scene->setSceneRect(0,0,800,600); // 设置场景范围

视图(QGraphicsView)

cpp

QGraphicsView *view = new QGraphicsView(scene);
view->setRenderHint(QPainter::Antialiasing);
view->setDragMode(QGraphicsView::RubberBandDrag); // 框选模式

图形项(QGraphicsItem)

cpp

class CustomItem : public QGraphicsItem {
    QRectF boundingRect() const override { ... }
    void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*) override { ... }
};
2. 高级功能实现

碰撞检测

cpp

QList<QGraphicsItem*> collisions = item->collidingItems(Qt::IntersectsItemBoundingRect);

图形特效

cpp

QGraphicsBlurEffect *effect = new QGraphicsBlurEffect;
effect->setBlurRadius(5);
item->setGraphicsEffect(effect);
3. 动画与状态机

属性动画

cpp

QPropertyAnimation *anim = new QPropertyAnimation(item, "rotation");
anim->setDuration(1000);
anim->setStartValue(0);
anim->setEndValue(360);
anim->setEasingCurve(QEasingCurve::OutBounce);
anim->start();

状态机设计

cpp

QStateMachine machine;
QState *state1 = new QState();
QState *state2 = new QState();
state1->assignProperty(button, "color", Qt::red);
state2->assignProperty(button, "color", Qt::green);
machine.addTransition(button, &QPushButton::clicked, state1, state2);

三、3D绘图(OpenGL集成)
1. OpenGL上下文管理

基础Widget

cpp

class GLWidget : public QOpenGLWidget, protected QOpenGLFunctions {
    void initializeGL() override { initializeOpenGLFunctions(); }
    void paintGL() override { glDrawArrays(...); }
};
2. 核心渲染技术

顶点缓冲对象(VBO)

cpp

GLfloat vertices[] = { ... };
QOpenGLBuffer vbo(QOpenGLBuffer::VertexBuffer);
vbo.create();
vbo.bind();
vbo.allocate(vertices, sizeof(vertices));

着色器编程

cpp

QOpenGLShaderProgram program;
program.addShaderFromSourceCode(QOpenGLShader::Vertex, vertShaderSrc);
program.link();
program.bind();
3. 高级渲染技术

Phong光照模型

glsl

// Fragment Shader
vec3 lightDir = normalize(lightPos - FragPos);
float diff = max(dot(normal, lightDir), 0.0);
vec3 diffuse = diff * lightColor;

纹理映射

cpp

QOpenGLTexture *texture = new QOpenGLTexture(QImage(":/texture.png"));
texture->bind();

四、实战建议
  1. 调试工具:使用qDebug() << scene->items().count()监控场景项数量
  2. 性能监控:重写QGraphicsItem::paint()时记录绘制时间
  3. 跨平台注意:OpenGL ES与桌面OpenGL的差异处理
  4. 资源管理:使用QOpenGLTexture::setAutoMipMapGeneration(true)自动生成Mipmap

五、学习资源
  • 官方文档:Qt Graphics View Framework
  • 进阶书籍:《Advanced Qt Programming》
  • 示例代码:Qt安装目录下的examples/openglexamples/widgets/painting

http://www.kler.cn/a/595244.html

相关文章:

  • ⭐算法OJ⭐二叉树的后序遍历【树的遍历】(C++实现)Binary Tree Postorder Traversal
  • 强大的AI网站推荐(第二集)—— V0.dev
  • 解释下Cumulative Layout Shift (CLS)以及如何优化?
  • JavaScript(JS)单线程影响速度
  • Linux:gsd-account进程异常内存泄漏排查
  • 背包问题——多重背包(C语言)
  • go中的文件、目录的操作
  • vscode/cursor中python运行路径设置 模块导入问题
  • 【AI论文】Being-0:一款配备视觉-语言模型与模块化技能的人形机器人智能体
  • TK矩阵系统:高效管理与智能化操作平台
  • 故事讲解设计模式:观察者模式
  • Kotlin标准函数库学习
  • csv文件格式和excel数据格式有什么区别
  • 游戏引擎学习第171天
  • JavaScript-函数、对象详解
  • 3.21学习总结
  • 【STM32】SPI通信协议W25Q64Flash存储器芯片(学习笔记)
  • Node.js 包与 npm 详解:使用 npm 的重要注意事项与最佳实践
  • python-56-基于Vue和Flask进行前后端分离的项目开发示例实战
  • C++算法代码-植物生长算法求解多目标车辆路径规划问题