除了使用固定的值,你可以乘height
和width
因素的根元素,决定你的元素成正比的根元素尺寸的大小.此外,您可以使用QML锚点.有了这个,您可以创建完全可扩展的GUI:
import QtQuick 1.0
Item {
id: root
// default size, but scalable by user
height: 300; width: 400
Rectangle {
id: leftPanel
anchors {
top: parent.top
left: parent.left
bottom: parent.bottom
}
width: root.width * 0.3
color: "blue"
}
Rectangle {
id: topPanel
anchors {
top: parent.top
left: leftPanel.right
right: parent.right
}
height: root.height * 0.2
color: "green"
}
Rectangle {
id: contentArea
anchors {
top: topPanel.bottom
left: leftPanel.right
right: parent.right
bottom: root.bottom
}
color: "white"
Text {
text: "Hi, I'm scalable!"
anchors.centerIn: parent
font.pixelSize: root.width * 0.05
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何通过所有环境都可用的纯QML获得屏幕分辨率.
要确定移动设备上的屏幕分辨率,您可以使用QML屏幕元素.
在桌面应用程序中,您可以使用C++获得屏幕分辨率(例如使用QDesktopWidget)并使其在QML中可用.