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