The Qt GUI module provides classes for windowing system integration, event handling, OpenGL and OpenGL ES integration, 2D graphics, basic imaging, fonts, and text. These classes are used internally by Qt's user interface technologies but can also be used directly, for example to write applications using low-level OpenGL ES graphics APIs.
对于编写用户界面的应用程序开发者而言,Qt 提供更高级 API,像 Qt Quick,比 Qt GUI 模块中找到的使能器更合适。
Qt GUI 模块最重要的类是 QGuiApplication and QWindow 。Qt 应用程序想要在屏幕上展示内容,就必须使用这些。 QGuiApplication 包含主事件循环,来自窗口系统和其它源的所有事件都会被处理和分派。它还处理应用程序的初始化及定稿。
The QWindow 类表示在底层窗口系统中的窗口。它提供许多虚函数以处理事件 ( QEvent ) 来自窗口系统,譬如:触摸输入、暴露、聚焦、击键及几何体变化。
The Qt GUI module contains classes for 2D graphics, imaging, fonts, and advanced typography.
A QWindow 的创建采用表面类型 QSurface::RasterSurface 可以用于组合 QBackingStore and QPainter ,Qt 高度优化的 2D 向量图形 API。 QPainter supports drawing lines, polygons, vector paths, images, and text. For more information, see 描绘系统 and 光栅窗口范例 .
Qt 可以加载和保存图像,使用 QImage and QPixmap 类。默认情况下,Qt 支持最常见的图像格式,包括 JPEG 和 PNG 等。用户可以添加支持其它格式凭借 QImageIOPlugin 类。更多信息,见 读写图像文件 .
Qt 中的排印是采用 QTextDocument ,使用 QPainter API 组合 Qt 的字体类,首要是 QFont 。偏好更低级 API 而不是文本和字体处理的应用程序,可以使用的的类像 QRawFont and QGlyphRun .
QRhi (Qt 渲染硬件接口) 是硬件加速图形 API 的抽象,譬如 OpenGL , OpenGL ES , Direct3D , Metal ,和 Vulkan .
As an alternative to using OpenGL or Vulkan directly to render to a QWindow , QRhi and the related classes provide a portable, cross-platform 3D graphics and compute API complemented by a shader conditioning and transpiling pipeline. This way applications can avoid directly depending on a single, and, in some cases, vendor or platform-specific 3D API.
Below is a list of the main RHI-related classes. These are complemented by a number of additional classes and structs.
见 RHI 窗口范例 for an introductory example of creating a portable, cross-platform application that performs accelerated 3D rendering onto a QWindow 使用 QRhi .
Working directly with QWindow is the most advanced and often the most flexible way of rendering with the QRhi API. It is the most low-level approach, however, and limited in the sense that Qt's UI technologies, widgets and Qt Quick, are not utilized at all. In many cases applications will rather want to integrate QRhi -based rendering into a widget or Qt Quick-based user interface. QWidget -based applications may choose to embed the window as a native child into the widget hierarchy via QWidget::createWindowContainer (), but in many cases QRhiWidget will offer a more convenient enabler to integrate QRhi -based rendering into a widget UI. Qt Quick provides its own set of enablers for extending the 2D/3D scene with QRhi -based custom rendering.
注意: The RHI family of APIs are currently offered with a limited compatibility guarantee, as opposed to regular Qt public APIs. See QRhi 了解细节。
Qt GUI 模块还包含一些数学类,以辅助 3D 图形相关的最常见数学运算。这些类包括 QMatrix4x4 , QVector2D , QVector3D , QVector4D ,和 QQuaternion .
QWindow 支持使用 OpenGL 和 OpenGL ES 进行渲染,取决于平台支持哪种。OpenGL 渲染的启用是通过设置 QWindow 的表面类型为 QSurface::OpenGLSurface ,选取格式属性采用 QSurfaceFormat ,然后创建 QOpenGLContext 以管理本机 OpenGL 上下文。此外,Qt 拥有 QOpenGLPaintDevice ,启用使用 OpenGL 加速 QPainter 渲染,还有方便类以简化 OpenGL 代码的编写,隐藏扩展处理的复杂性及 OpenGL ES 2 和桌面 OpenGL 之间的差异。方便类包括 QOpenGLFunctions that lets an application use all the OpenGL ES 2 functions on desktop OpenGL without having to manually resolve the OpenGL function pointers. This enables cross-platform development of applications targeting mobile or embedded devices, and provides classes that wrap native OpenGL functionality in a simpler Qt API:
最后,为更好地支持 OpenGL 较新版本 (3.0 及更高版本),版本化函数包裹器机制也可用:QOpenGLFunction_N_N 系列类暴露 OpenGL 版本和配置文件给定的所有函数,允许轻松开发依赖现代、仅限桌面 OpenGL 特征的桌面应用程序。
更多信息,见 OpenGL 窗口范例 .
A QWindow 的创建采用 QSurface::OpenGLSurface 可以用于组合 QPainter and QOpenGLPaintDevice to have OpenGL hardware-accelerated 2D graphics by sacrificing some of the visual quality.
Qt GUI 支持 Vulkan API。Qt 应用程序要求存在 LunarG Vulkan SDK .
在 Windows,SDK 设置环境变量
VULKAN_SDK
,会被检测到通过
configure
脚本。
在 Android,NDK 的 API Level 24 添加了 Vulkan 头。
The main relevant classes for low-level Vulkan support are:
此外, QVulkanWindow provides a convenience subclass of QWindow that makes it easier to get started with implementing Vulkan-based rendering targeting a QWindow . Using this helper class is completely optional; applications with more advanced Vulkan-based renderers may instead want to use a QWindow 采用 QSurface::VulkanSurface type directly.
更多信息,见 Hello Vulkan 小部件范例 和 Hello Vulkan Triangle Example .
Qt GUI 包括对拖放的支持。 拖放 概述有更多信息。