在QML中扩展和定位

问题描述 投票:0回答:1

我正在开发一个运行到QQuickWidget的Qt5.11.1 QML应用程序。调整大小模式设置为SizeRootObjectToView。因此,每当我调整QMainWindow的大小时,我看到我的QML根对象也会缩放。

在里面我有一些图像锚定来填充父母,他们按预期缩放。相反,我对较小的图像或文本有问题,应保持相同的相对位置和大小。

当比例为1:1时,我从绝对位置和大小开始。示例:根项目的大小为1920x1080像素,我必须将其他项目(主要是图像和文本)放置到给定的坐标。

当根改变其大小时,所有元素都应该遵循它。我试过这个:

import QtQuick 2.11
import QtQuick.Controls 2.4
import QtGraphicalEffects 1.0

Rectangle {
    id: root
    visible: true
    color: "black"

    property real ratio: root.width / 1920

    readonly property real x_CenterGauge: 502
    readonly property real y_CenterGauge: 489

    Image {
        x: x_CenterGauge * ratio
        y: y_CenterGauge * ratio
        scale: ratio
    }
}

但这不起作用,因为当我调整窗口大小时,root.width属性(反过来ratio)不会改变。但它实际上调整了根元素的大小,因为任何锚定的项目也会调整大小!只有在我最大化/最小化窗口时才会进行更改。

我读了this articlethis question,但我仍然不明白如何处理QML中的resising。

qt resize qml qt5 scaling
1个回答
0
投票

在测试中,属性规模似乎是一个问题通过修改宽度和高度而不是它解决了问题

Rectangle {
    id: root
    visible: true
    color: "black"

    property real ratio: root.width / 1920

    readonly property real x_CenterGauge: 202
    readonly property real y_CenterGauge: 489

    Image {
        x: root.x_CenterGauge * root.ratio
        y: root.y_CenterGauge * root.ratio

        width: implicitWidth * root.ratio
        height: implicitHeight * root.ratio
        //scale: ratio
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.