最小 QML
Minimal QML 是一个简单的示例,演示如何用 QML 编写 Wayland 合成器。
Minimal QML 是一个桌面风格的 Wayland 合成器示例,以尽可能少的代码实现了完整的Qt Wayland Compositor 。该合成器使用Qt Quick 和 QML 实现。
WaylandCompositor 对象
合成器的顶层项目是WaylandCompositor 。它代表 Wayland 服务器本身,并管理与客户端的连接。
默认情况下,服务器支持与客户端通信的核心 Wayland 协议。但通常情况下,你还需要支持该协议的一个或多个扩展。这就为客户端提供了更多工具,以影响其在窗口系统中的角色。
Qt 支持多种标准和常见的扩展。此外,只要能在客户端和服务器代码中添加支持,创建和支持自定义扩展也很容易。
外壳扩展
通常情况下,合成器至少支持一种shell 扩展。扩展可以作为WaylandCompositor 对象的直接子对象实例化,从而添加到合成器中。它们会自动添加到extensions
属性中,并在客户端连接时广播给客户端。
WlShell { onWlShellSurfaceCreated: (shellSurface) => shellSurfaces.append({shellSurface: shellSurface}); } XdgShell { onToplevelCreated: (toplevel, xdgSurface) => shellSurfaces.append({shellSurface: xdgSurface}); } IviApplication { onIviSurfaceCreated: (iviSurface) => shellSurfaces.append({shellSurface: iviSurface}); }
最小 QML示例支持三种不同的外壳:WlShell、XdgShell 和IviApplication 。
客户端可以连接到其中任何一个,它将被用作客户端与合成器之间就某些事情(如创建新窗口、协商大小等)进行通信的通道。
当客户端创建新窗口时,其活动扩展会收到一个信号。该信号包含一个ShellSurface 参数。根据接收信号的扩展,该参数将分别属于ShellSurface :WlShellSurface、XdgSurface 或IviSurface 的子类。
ShellSurface 可用于访问特定曲面的外壳扩展功能。在最小 QML示例中,我们只想把客户端添加到场景中。为了记录新窗口的存在,我们将其添加到一个简单的ListModel 中,以便妥善保存。
ListModel { id: shellSurfaces }
创建场景
大部分必要的合成器代码已经准备就绪。最后一步是确保应用程序在屏幕上实际可见。
对于所有合成器,我们必须定义至少一个输出。具体做法是实例化一个WaylandOutput 对象,作为WaylandCompositor 的直接子对象。如果只有一个输出,它将代表系统的主屏幕。(如果有多个屏幕,也可以创建多个WaylandOutput 对象来处理多个屏幕)。有关详情,请参阅多屏幕示例)。
WaylandOutput { sizeFollowsWindow: true window: Window { width: 1024 height: 768 visible: true
在WaylandOutput 中,我们创建了一个窗口,作为场景的容器。在示例中,我们赋予窗口一个尺寸。如果合成器作为应用程序运行在另一个支持自定义大小窗口的窗口系统中,则使用该大小。在嵌入式设备上的典型用例中,合成器是唯一运行的显示服务器,它很可能运行在全屏平台插件上(如eglfs
),这里设置的大小并不重要。
最后一步是为每个已创建的ShellSurface 对象创建项目。为此,我们可以使用ShellSurfaceItem 类。
Repeater { model: shellSurfaces // ShellSurfaceItem handles displaying a shell surface. // It has implementations for things like interactive // resize/move, and forwarding of mouse and keyboard // events to the client process. ShellSurfaceItem { shellSurface: modelData onSurfaceDestroyed: shellSurfaces.remove(index) } }
我们为模型中的每个外壳表面创建一个ShellSurfaceItem ,并将其分配给shellSurface
属性。此外,我们还要确保在外壳表面销毁时更新模型。这可能发生在客户端手动关闭窗口、退出或崩溃时。
这就是使用Qt Quick 和 QML 创建功能性 Wayland 合成器所需的全部代码。如需了解另一个用 QML 编写但功能更多的合成器示例,请参阅Fancy Compositor 示例。
© 2025 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.