概述:Compositor
“概述 Compositor” 展示了如何在网格中切换客户端。
简介
“Compositor 概述”演示了如何从当前已连接客户端的网格中选择并激活一个应用程序。

有关使用 Qt Wayland Compositor ,请参阅“最简 Qml”示例。
应用程序网格
在此示例中,Compositor 支持两种不同的运行模式:
- 全屏模式:此时单个应用程序窗口占据整个合成器窗口,且可进行交互。
- 概览模式:所有应用程序窗口以网格形式显示。点击网格中的某个窗口即可将其选中。合成器随后进入全屏模式,显示所选的应用程序窗口。
当客户端连接到合成器并创建顶级表面时,该表面将连接到一个外壳扩展。本示例仅支持XdgShell 扩展,因此客户端将连接到该扩展。
XdgShell {
onToplevelCreated: (toplevel, xdgSurface) => {
toplevels.append({xdgSurface});
toplevel.sendFullscreen(Qt.size(win.pixelWidth, win.pixelHeight));
}
}对于每个表面,我们会指示客户端将其配置为全屏模式。此外,这些表面会被添加到ListModel 中以便于访问。
Repeater 采用此模型,在Grid 中创建ShellSurfaceItems 。Grid 组件将项目以网格形式排列。
Repeater {
model: toplevels
Item {
width: win.width
height: win.height
ShellSurfaceItem {
anchors.fill: parent
shellSurface: xdgSurface
onSurfaceDestroyed: toplevels.remove(index)
}
MouseArea {
enabled: grid.overview
anchors.fill: parent
onClicked: {
grid.selected = index;
grid.overview = false;
}
}
}
}对于每个项目,我们都会创建一个MouseArea ,该组件覆盖该项目并拦截所有鼠标和触摸输入。此功能仅在合成器处于概览模式时生效,并会激活被点击的应用程序。
当合成器进入全屏模式时,会继续使用同一个Grid 组件,但会对其进行缩放和平移,使其位于所选单个单元格能填满合成器窗口的位置。其设计理念是“放大”所选单元格,让用户能够与其中包含的应用程序进行交互。
transform: [
Scale {
xScale: grid.overview ? (1.0/grid.columns) : 1
yScale: grid.overview ? (1.0/grid.columns) : 1
Behavior on xScale { PropertyAnimation { easing.type: Easing.InOutQuad; duration: 200 } }
Behavior on yScale { PropertyAnimation { easing.type: Easing.InOutQuad; duration: 200 } }
},
Translate {
x: grid.overview ? 0 : win.width * -grid.selectedColumn
y: grid.overview ? 0 : win.height * -grid.selectedRow
Behavior on x { PropertyAnimation { easing.type: Easing.InOutQuad; duration: 200 } }
Behavior on y { PropertyAnimation { easing.type: Easing.InOutQuad; duration: 200 } }
}
]窗口底部有一个按钮,用于在不同模式之间切换。当合成器处于全屏模式时,可通过此按钮恢复应用程序网格。
本示例展示了一种让合成器在不同模式下可视化客户端的方法。实现类似效果的另一种方法是创建多个引用同一表面的Qt Quick 项。请参阅“多输出”示例以了解演示。
© 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.