本页内容

跨屏显示

“跨屏显示”是一个示例,演示了如何让 Wayland 客户端跨多个屏幕显示。

简介

“跨屏显示”是一个 Wayland 合成器示例,它将客户端窗口最大化显示在顶部和底部的屏幕上。

两个合成器窗口,其中一个窗口横跨两个显示器

有关使用 Qt 创建 Qt Wayland Compositor ,请参阅“Minimal Qml”示例。

支持多显示器

在 Qt Wayland Compositor 中,一个屏幕由WaylandOutput 表示,而Window用于包含 Qt Quick 场景,该场景同时呈现客户端和合成器的用户界面。

在此示例中,通过在主屏幕上创建两个窗口来模拟多屏幕设置,但可以轻松修改代码以支持多个物理屏幕。

// Enable the following to make the output target an actual screen,
// for example when running on eglfs in a multi-display embedded system.

// screen: Qt.application.screens[0]

由于每个Window代表一个独立的 Qt Quick 场景,因此我们需要一个技巧,才能让相同的客户端内容同时显示在两个窗口中。实现此目的的方法 Qt Wayland Compositor 方法是为同一客户端内容创建两个视图:一个用于“顶部”窗口,另一个用于“底部”窗口。这两个视图共享对同一底层图形缓冲区的引用。这使我们能够将客户端画面的不同区域复制到各自的窗口中。

const topItem = chromeComponent.createObject(topSurfaceArea, {
    shellSurface
});

const bottomItem = chromeComponent.createObject(bottomSurfaceArea, {
    shellSurface,
    y: Qt.binding(function() { return -topSurfaceArea.height;})
});

当客户端连接到shell 扩展 XdgShell 时,我们会创建两个对画面的引用。其中一个被添加到“顶部”输出中,另一个被添加到“底部”输出中。 “底部”输出中的项目还会获得一个与“顶部”输出高度相对应的偏移量。这确保了显示在“底部”输出中的客户端画布部分,从“顶部”输出结束的位置开始。

const height = topSurfaceArea.pixelHeight + bottomSurfaceArea.pixelHeight;
const width = Math.max(bottomSurfaceArea.pixelWidth, topSurfaceArea.pixelWidth);
const size = Qt.size(width, height);
toplevel.sendFullscreen(size);

此外,我们会指示客户端调整其画布大小,使其同时填满顶部和底部窗口。最终结果是客户端横跨两个窗口,即“屏幕”。

从多个项目引用同一客户端表面是一种用途广泛的工具。若要了解可将窗口在不同屏幕间移动的桌面风格合成器的演示,请参阅“多屏幕”示例。

“多输出”示例展示了如何将客户端表面显示在具有不同尺寸及其他属性的多个输出设备上。

注意:为了在 同一合成器中支持多个 Wayland 输出,必须在 构建 `QGuiApplication ` 对象之前设置 `Qt::AA_ShareOpenGLContexts ` 属性。

示例项目 @ code.qt.io

© 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.