Qt 平台抽象
Qt 平台抽象(QPA)是 Qt 中的主要平台抽象层。
该 API 可通过QPlatform* 类前缀识别,通常用于实现更高层次的类,这些类常见于 Qt GUI,例如用于窗口系统集成的QPlatformWindow (通过QWindow 暴露),或用于更深入的平台主题和集成的QPlatformTheme (通过QStyleHint和QStyle 暴露)。
QPA 类不提供源代码或二进制兼容性保证,这意味着该 API 仅保证与开发时所基于的 Qt 版本兼容。不过,API 变更仅会在次要版本(如 5.1、5.2 等)中进行。
API 概述
- 窗口系统集成
- QPlatformIntegration
- QPlatformAccessibility
- QPlatformBackingStore
- QPlatformClipboard
- QPlatform光标
- QPlatformDrag
- QPlatformFontDatabase
- QPlatformGraphicsBuffer
- QPlatformInputContext
- QPlatformOffscreenSurface
- QPlatformOpenGLContext
- QPlatformScreen
- QPlatformServices
- QPlatform共享图形缓存
- QPlatformSurface
- QPlatformWindow
- 样式/主题
- QPlatformTheme
- 平台配色方案
- 平台字体
- 主题提示
- 原生对话框和菜单
- QPlatformDialogHelper
- QPlatformMenu
- QPlatformMenuBar
- QPlatformMenuItem
注意:除了 QPA API 之外,Qt 中还有许多其他平台抽象,例如 RHI 中的事件分发器抽象 Qt Core 中的事件分发器抽象,以及 RHI 中的渲染硬件抽象。QPA API 主要侧重于窗口系统集成及相关支持。
QPA 插件
上述接口的具体实现位于 QPA 插件(或称平台插件)中,为 Qt 运行的各种平台提供了支持。QPA 插件通过继承QPlatform* 类来实现。
下表总结了可用的平台插件:
| 插件名称 | 插件类名称 | 描述 |
|---|---|---|
qandroid | QAndroidPlatformIntegration | Android 支持 |
qbsdfb | QBsdFbIntegration | BsdFb 支持 |
qcocoa | QCocoaIntegrationPlugin | macOS 支持 |
qdirect2d | QWindowsDirect2DIntegrationPlugin | 与qwindows 插件类似,但将栅格绘制引擎替换为基于Direct2D的绘制引擎,适用于QPixmap 和QBackingStore 。 |
qdirectfb | QDirectFbIntegrationPlugin | DirectFB 以表面为核心,这些表面相当于QPaintDevice 。 |
qeglfs | QEglFSIntegrationPlugin | 针对嵌入式Linux设备的EGLFS支持。 |
qhaiku | QHaikuIntegration | Haiku 支持 |
qios | QIOSIntegrationPlugin | iOS 支持 |
qlinuxfb | QLinuxFbIntegrationPlugin | 针对嵌入式 Linux 设备的LinuxFB支持。 |
qvnc | QVncIntegration | VNC 支持 |
qwayland |
| Wayland 支持 |
qwindows | QWindowsIntegrationPlugin | Windows 支持 |
qxcb | QXcbIntegrationPlugin | X Window System (X11) 支持 |
选择 QPA 插件
Qt 将根据平台选择默认的 QPA 插件。您可以通过环境变量 `QT_QPA_PLATFORM ` 和命令行参数 `-platform ` 来覆盖此默认设置。更多详细信息请参阅QGuiApplication 。
您可以传递一个以分号分隔的列表来安装插件。在这种情况下,安装程序将遍历列表中的项目,并安装第一个可用的插件。
示例:
-qpa xcb;wayland上述命令行会先检查xcb 插件是否可用,若不可用,则继续检查wayland插件。
编写 QPA 插件
目前尚无关于编写 QPA 插件的详细教程文档。不过,Qt 随附了两个用于测试和其他特殊用途的简易插件,您可以将其作为示例:
qtbase/src/plugins/platforms/minimal/qtbase/src/plugins/platforms/minimalegl/
qminimal 插件用于链接到 Qt GUI,但不需要与窗口系统集成的工具(例如,qmlplugindump )。它还支持根据环境变量将窗口内容转储到图像文件中的诊断功能。
© 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.