QML在多屏显示同一个用户界面

把主界面的内容复制到另外一个屏幕的窗口里面,以便于展示。如何实现这个功能呢?搜索了全网,最后找了一个比较合适的方案。参考分屏显示
里面有两种方法,我采用第二种方案。

import QtQuick 2.15
import QtQuick.Window 2.15

Window {
    id: mainWindow

    title: qsTr("Main Window")
    visible: true
    width: 400
    height: 200
    color: "#0F0"

    Rectangle {
        id: viewport
        color: "#00F"
        width: parent.width/2
        height: parent.height/2
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.verticalCenter: parent.verticalCenter
    }

    onFrameSwapped: {
        viewport.grabToImage(function(result) {
            projectionWindow.frame = String(result.url);
        });
    }

    Window {
        id: projectionWindow

        property int screenID: 1
        property alias frame: img.source

        transientParent: mainWindow
        visible: true
        x: Qt.application.screens[screenID].virtualX
        y: Qt.application.screens[screenID].virtualY
        width: Qt.application.screens[screenID].desktopAvailableWidth
        height: Qt.application.screens[screenID].desktopAvailableHeight
        flags: Qt.FramelessWindowHint
        color: "#000"
        visibility: Window.Maximized

        Image {
            id: img
            anchors.fill: parent
            fillMode: Image.PreserveAspectFit
            // Performance tweaks
            asynchronous: true
            cache: false
        }
    }
}

一些注意的点:

1.Image控件的cache必须设置为true,不然随着时间的推移,会占很多内存。(刚开始我还以为我写得C++代码内存泄漏了)。

2.这种方法不能把弹出窗口、类似Popup这样的控件复制过去,所以得在复制的窗口再增加一个Image,复制窗口和控件,还得设置Image的位置。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值