QML中元素的大小

Gro*_*ovy 7 qt qml

我是QML的新手.据我所知,所有元素都有相关的宽度和高度,这决定了它们的大小.如果用户更改屏幕分辨率,则最终输出看起来很奇怪.有没有办法根据屏幕分辨率动态控制元素的大小?

hid*_*bit 8

除了使用固定的值,你可以乘heightwidth因素的根元素,决定你的元素成正比的根元素尺寸的大小.此外,您可以使用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中可用.