图形

Qt 5 中的图形处理首要透过命令式 QPainter API,或透过 Qt 的声明式 UI 语言、Qt Quick 及其场景图形后端。Qt 5 的图形能力还包括支持打印,及加载和保存各种图像格式。

2D 图形与 QPainter

QPainter 提供 API 为绘制向量图形、文本和图像在不同表面,或 QPaintDevice 实例,譬如 QImage , QOpenGLPaintDevice , QWidget ,和 QPrinter 。实际绘制发生在 QPaintDevice 's QPaintEngine 。软件光栅化器和 OpenGL (ES) 2.0 后端是 2 个最重要的 QPaintEngine 实现。光栅描绘引擎是 Qt 的软件光栅化器,并被用于绘制在 QImage or QWidget 。与 OpenGL 描绘引擎相比,它的优势在于启用抗锯齿时的高品质和完整特征集。

最重要的渲染目标为 QPainter 是:

QPainter 和相关类属于 Qt GUI 模块。

OpenGL 和 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.

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 场景图形

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 can be mixed with raw OpenGL rendering by connecting to the signals QQuickWindow::beforeRendering () 或 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 也可以渲染使用 Qt Quick 2D Renderer 。此光栅描绘引擎在没有 OpenGL 的平台渲染 Qt Quick 应用程序。

打印

Qt 支持直接打印到本地或网络的实际打印机,及产生 PDF 输出。如何采用 Qt 进行打印的详细描述在 Qt Print Support 页面。

图像

Qt 支持图像的方便读取、写入及操纵透过 QImage 类。此外,对于如何更细粒度控制图像的加载或保存,可以使用 QImageReader and QImageWriter 类。为添加由 Qt 所提供的其它图像格式的支持,可以创建图像格式插件通过使用 QImageIOHandler and QImageIOPlugin .

读取和写入图像文件 页面,了解更多信息。

另请参阅 描绘系统 .