您的位置:首页 > 运维架构

QOpenGLWidget绘制2D的方法

2017-08-13 13:35 429 查看
补充:必须在非核心模式的opengl才能实现同时使用gl原生函数和QPainter绘制。核心模式的opengl使用QPainter会导致QOpenGLWidget什么都显示不出来。
参考:https://stackoverflow.com/questions/27422928/qopenglwidget-and-qpainter-can-t-render-2d-and-3d-at-the-same-time

可以重新实现paintGL(),在其中通过QPainter绘图。通过update()重绘。
也可以使用通常QWidget的paintEvent()方法实现绘图,通过update()重绘。
下面是官方文档的节选:
Painting Techniques
As described above, subclassQOpenGLWidget to render pure 3D content in the following way:
Reimplement the
initializeGL() andresizeGL() functions to set up the OpenGL state and provide a perspective transformation.
Reimplement
paintGL() to paint the 3D scene, calling only OpenGL functions.
It is also possible to draw 2D graphics onto aQOpenGLWidget subclass usingQPainter:
In paintGL(), instead of issuing OpenGL commands, construct aQPainter
object for use on the widget.
Draw primitives using
QPainter's member functions.
Direct OpenGL commands can still be issued. However, you must make sure these are enclosed by a call to the painter's beginNativePainting() and endNativePainting().
When performing drawing usingQPainter only, it is also possible to perform the painting
like it is done for ordinary widgets: by reimplementingpaintEvent().
Reimplement the
paintEvent() function.
Construct a
QPainter object targeting the widget. Either pass the widget to the constructor or theQPainter::begin() function.
Draw primitives using
QPainter's member functions.
Painting finishes then the
QPainter instance is destroyed. Alternatively, callQPainter::end() explicitly.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: