Graphics
Qt 5中的图形主要通过强制性的QPainter API或通过Qt的声明性UI语言Qt Quick及其场景图后端来完成。 Qt 5的图形功能还包括对打印的支持,以及各种图像格式的加载和保存。
QPainter的2D图形
QPainter provides API for drawing vector graphics, text and images onto different surfaces, or QPaintDevice instances, such as QImage, QOpenGLPaintDevice, QWidget, and QPrinter. 实际绘制发生在QPaintDevice的QPaintEngine中。 软件光栅化器和OpenGL(ES)2.0后端是两个最重要的QPaintEngine实现。 光栅绘制引擎是Qt的软件光栅化器,用于在QImage或QWidget上绘制时使用。 启用抗锯齿功能后,它具有超过OpenGL绘制引擎的优势是其高质量,并具有完整的功能集。
QPainter的最重要的渲染目标是:
- QImage-具有直接像素访问权限的独立于硬件的图像表示。 QPainter将使用软件光栅化器绘制QImage实例。
- QPixmap-适合在屏幕上显示的图像表示。 QPainter将主要使用软件光栅化器绘制QPixmap实例。
- QOpenGLPaintDevice-一种绘制设备以渲染到当前OpenGL(ES)2.0上下文。 QPainter将使用硬件加速的OpenGL调用来绘制QOpenGLPaintDevice实例。
- QBackingStore-顶级窗口的后缓冲区。 QPainter将主要使用软件光栅化器来绘制QBackingStore实例。
- QWidget-Qt之前的Quick用户界面类的基类。 QPainter将使用QBackingStore渲染小部件。
- QOpenGLWidget-画家也可以在QOpenGLWidget上打开。 提供此功能是为了方便,因为从技术上讲,这与使用QOpenGLPaintDevice没什么不同。
OpenGL and 3D
OpenGL is the most widely adopted graphics API for hardware accelerated and 3D graphics, implemented on all desktop platforms and almost every mobile and embedded platform. The Qt library contains a number of classes that help users integrate OpenGL into their applications.
- OpenGL in Qt GUI - An overview of how OpenGL integrates with the Qt GUI module.
- QOpenGLWidget is a widget that allows adding OpenGL scenes into QWidget-based user interfaces.
- OpenGL and Qt Quick 2.0 - How to integrate OpenGL into a the Qt Quick 2.0 scene graph.
- www.khronos.org/opengl - The official OpenGL pages.
- Qt Canvas 3D - An add-on module that provides a way to make OpenGL-like 3D drawing calls from Qt Quick using JavaScript.
Prior to Qt 5.0, OpenGL support in Qt was handled by the Qt OpenGL module. This module is still present, but new code should aim to use the new classes in the Qt GUI module. The classes are easily distinguisible based on their names: Classes with the QGL
prefix should not be used. Instead, prefer the ones starting with QOpenGL
.
Qt Quick Scene Graph
Qt Quick 2 introduces an OpenGL (ES) 2.0 scene graph for rendering. It generally improves the performance of Qt Quick 2 significantly compared to the QGraphicsView/QPainter-based approach used in earlier versions.
The scene graph is a graphical representation of the Item scene. It can be thought of as a graphical deep copy, an independent structure that contains enough information to render all the items. Once it has been set up, it can be manipulated and rendered independently of the state of the items. On many platforms, the scene graph will even be rendered on a dedicated render thread while the GUI thread is preparing the next frame's state.
The scene graph is used when you import QtQuick 2.x in your QML file, and use QQuickView to run it.
- Qt Quick Scene Graph - Overview of the Qt Quick Scene Graph architecture.
- Scene Graph and Rendering - Breakdown of the rendering of each frame.
Qt Quick can be mixed with raw OpenGL rendering by connecting to the signals QQuickWindow::beforeRendering() or QQuickWindow::afterRendering() which are emitted before and after the Qt Quick scene graph is rendered, respectively. There signals are emitted from the render thread (when applicable), and the connections need to be direct.
Qt Quick can also be rendered using Qt Quick 2D Renderer. This raster paint engine enables rendering Qt Quick applications on platforms that do not have OpenGL.
Printing
Qt supports printing both directly to actual printers, locally or on the network, as well as producing PDF output. How to do printing with Qt is described in detail on the Qt Print Support page.
Images
Qt supports convenient reading, writing, and manipulating of images through the QImage class. In addition, for more fine grained control of how images are loaded or saved, you can use the QImageReader and QImageWriter classes respectively. To add support for additional image formats, outside of the ones provided by Qt, you can create image format plugins by using QImageIOHandler and QImageIOPlugin.
See the Reading and Writing Image Files page for more information.
See also Paint System.