【问题标题】:Any type of positioners doesn't work with user-defined element任何类型的定位器都不适用于用户定义的元素
【发布时间】:2017-11-13 07:15:16
【问题描述】:

有一个传感器元件的代码。它就像其他传感器类型元素的基类:

Rectangle {
    property real value: 0

    property alias imageWidth: image.width
    property alias imageSource: image.source
    property alias displayWidth: display.width

    width: Math.max(image.width, display.width)
    height: image.height + display.height

    border.width: 1

    Rectangle {
        id: display
        width: 40
        height: 20
        border.width: 1
        color: "lightgreen"

        Text {
            anchors.centerIn: parent
            text: value
            font.pixelSize: parent.width * 0.3;
        }
    }

    Image {
        id: image
        width: display.width * 0.5
        anchors.top: display.bottom
        anchors.horizontalCenter: display.horizontalCenter
        source: "qrc:/images/temperature.png"
        fillMode: Image.PreserveAspectFit
    }
}

如果我将传感器放在Column 中,它们的位置是正确的。

派生类型的实现-Pressure:

Item {
    Sensor {
        imageSource: "qrc:/images/pressure.png"
        imageWidth: displayWidth * 0.4
    }
}

但如果我将Pressure 传感器放在RowColumn 中,所有传感器就会相互重叠。

Column {
        spacing: 10
        Pressure {}
        Pressure {}
        Pressure {}
}

Pressures and Sensors comparison (image)

你能解释一下它有什么问题吗?

【问题讨论】:

    标签: qt user-interface qml qtquick2


    【解决方案1】:

    哦,看来我自己找到了答案。

    Items 在像Pressure 这样的派生类中没有自己的宽度和高度。如果我删除Item,它可以正常工作:

    Sensor {
        imageSource: "qrc:/images/pressure.png"
        imageWidth: displayWidth * 0.4
    }
    

    【讨论】:

    • 嗨,作为一个小想法:您可能想阅读有关 implicitWidth/Height 的内容 - 从语义上看,您想要的不是必需的,但通常它是好的练习在根节点设置隐含值,而不是直接width/height
    猜你喜欢
    • 2016-09-06
    • 1970-01-01
    • 2013-08-24
    • 2021-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-16
    • 1970-01-01
    相关资源
    最近更新 更多