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();
四、实战建议
- 调试工具:使用
qDebug() << scene->items().count()
监控场景项数量 - 性能监控:重写
QGraphicsItem::paint()
时记录绘制时间 - 跨平台注意:OpenGL ES与桌面OpenGL的差异处理
- 资源管理:使用
QOpenGLTexture::setAutoMipMapGeneration(true)
自动生成Mipmap
五、学习资源
- 官方文档:Qt Graphics View Framework
- 进阶书籍:《Advanced Qt Programming》
- 示例代码:Qt安装目录下的
examples/opengl
和examples/widgets/painting