本页内容

QOpenGLPaintDevice Class

QOpenGLPaintDevice 类支持通过 `QPainter` 向 OpenGL 上下文中绘制图形。更多内容...

头文件: #include <QOpenGLPaintDevice>
CMake: find_package(Qt6 REQUIRED COMPONENTS OpenGL)
target_link_libraries(mytarget PRIVATE Qt6::OpenGL)
qmake: QT += opengl
继承自: QPaintDevice

公共函数

QOpenGLPaintDevice()
QOpenGLPaintDevice(const QSize &size)
QOpenGLPaintDevice(int width, int height)
virtual ~QOpenGLPaintDevice()
QOpenGLContext *context() const
qreal dotsPerMeterX() const
qreal dotsPerMeterY() const
virtual void ensureActiveTarget()
bool paintFlipped() const
void setDevicePixelRatio(qreal devicePixelRatio)
void setDotsPerMeterX(qreal dpmx)
void setDotsPerMeterY(qreal dpmy)
void setPaintFlipped(bool flipped)
void setSize(const QSize &size)
QSize size() const

重新实现的公共函数

virtual QPaintEngine *paintEngine() const override

受保护函数

QOpenGLPaintDevice(const QOpenGLPaintDevice &)
QOpenGLPaintDevice &operator=(const QOpenGLPaintDevice &)

已重新实现的受保护函数

virtual int metric(QPaintDevice::PaintDeviceMetric metric) const override

详细说明

QOpenGLPaintDevice 使用当前的QOpenGL 上下文来渲染 `QPainter ` 绘制命令。该上下文在构造时被捕获。它要求支持 OpenGL (ES) 2.0 或更高版本。

性能

QOpenGLPaintDevice 几乎总是采用硬件加速,其速度可能远快于软件光栅化。但是,它对状态变化更为敏感,因此需要仔细安排绘制命令的顺序,以实现最佳性能。

抗锯齿与质量

OpenGL 绘制引擎中的抗锯齿采用多采样方式实现。大多数硬件在执行多采样时需要消耗显著更多的内存,且生成的画质无法与软件绘制引擎相媲美。OpenGL 绘制引擎的优势在于其性能,而非视觉渲染质量。

状态变更

当使用QPainter 向QOpenGLPaintDevice绘制时,绘制引擎会修改当前OpenGL上下文的状态以满足其需求。应用程序不应依赖于OpenGL状态会被重置为原始状态,特别是当前着色器程序、OpenGL视口、纹理单元和绘制模式。

混合使用 QPainter 和 OpenGL

在混合使用QPainter 和OpenGL时,必须通知QPainter ,告知其OpenGL状态可能已被篡改,以便它能够恢复其内部状态。具体实现方法是在开始OpenGL渲染前调用QPainter::beginNativePainting(),并在完成后调用QPainter::endNativePainting()。

安全注意事项

警告: 通过 QOpenGLPaintDevice 渲染的所有 内容均 应为可信内容。矢量几何图形、图像及其他绘图数据在被处理(可能被修改)后,会传递给底层的 OpenGL 实现。建议应用程序开发人员在渲染(传递)用户提供的内容之前,仔细考虑其潜在影响。

另请参阅 OpenGL 窗口示例。

成员函数文档

QOpenGLPaintDevice::QOpenGLPaintDevice()

创建一个 QOpenGLPaintDevice 对象。

QOpenGLPaintDevice 仅对当前上下文有效。

另请参阅 ` QOpenGLContext::currentContext()`。

[explicit] QOpenGLPaintDevice::QOpenGLPaintDevice(const QSize &size)

使用给定的size 创建一个QOpenGLPaintDevice。

该 QOpenGLPaintDevice 仅对当前上下文有效。

另请参阅 QOpenGLContext::currentContext()。

QOpenGLPaintDevice::QOpenGLPaintDevice(int width, int height)

使用给定的width 和height 构造一个QOpenGLPaintDevice。

该 QOpenGLPaintDevice 仅对当前上下文有效。

另请参阅 QOpenGLContext::currentContext()。

[protected delete] QOpenGLPaintDevice::QOpenGLPaintDevice(const QOpenGLPaintDevice &)

复制并构造一个QOpenGLPaintDevice 实例。此函数已被删除。

[virtual noexcept] QOpenGLPaintDevice::~QOpenGLPaintDevice()

销毁“QOpenGLPaintDevice ”号。

QOpenGLContext *QOpenGLPaintDevice::context() const

返回与绘制设备关联的 OpenGL 上下文。

qreal QOpenGLPaintDevice::dotsPerMeterX() const

返回水平方向上每米对应的像素数。

另请参阅 setDotsPerMeterX()。

qreal QOpenGLPaintDevice::dotsPerMeterY() const

返回垂直方向上每米对应的像素数。

另请参阅 setDotsPerMeterY()。

[virtual] void QOpenGLPaintDevice::ensureActiveTarget()

提供此虚拟方法作为回调,以便在不同的QOpenGLPaintDevice 实例交替发出绘制调用时,能够重新绑定目标帧缓冲区对象或上下文。

beginNativePainting() 也会触发此方法。

() 也会触发此方法。

[override virtual protected] int QOpenGLPaintDevice::metric(QPaintDevice::PaintDeviceMetric metric) const

重写了:QPaintDevice::metric (QPaintDevice::PaintDeviceMetric metric)const。

[override virtual] QPaintEngine *QOpenGLPaintDevice::paintEngine() const

重新实现了:QPaintDevice::paintEngine() const。

bool QOpenGLPaintDevice::paintFlipped() const

如果绘制对象沿 Y 轴翻转,则返回true 。

另请参阅 setPaintFlipped()。

void QOpenGLPaintDevice::setDevicePixelRatio(qreal devicePixelRatio)

将绘制设备的设备像素比设置为devicePixelRatio 。

void QOpenGLPaintDevice::setDotsPerMeterX(qreal dpmx)

将水平方向每米像素数设置为dpmx 。

另请参阅 dotsPerMeterX()。

void QOpenGLPaintDevice::setDotsPerMeterY(qreal dpmy)

将垂直方向上每米对应的像素数设置为dpmy 。

另请参阅 dotsPerMeterY()。

void QOpenGLPaintDevice::setPaintFlipped(bool flipped)

将绘图是否应围绕 Y 轴翻转的设置设为flipped 。

另请参阅 paintFlipped()。

void QOpenGLPaintDevice::setSize(const QSize &size)

将绘制设备的像素大小设置为size 。

另请参阅 size()。

QSize QOpenGLPaintDevice::size() const

返回绘制设备的像素大小。

另请参阅 ` setSize()`。

[protected delete] QOpenGLPaintDevice &QOpenGLPaintDevice::operator=(const QOpenGLPaintDevice &)

将other 复制并赋值给此QOpenGLPaintDevice 实例。该函数已被删除。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.