qml item - KerwinKoo/KerwinKoo.github.io GitHub Wiki

Qt5.4 QML Item类

简介

Item 是 Qt Quick 中所有可视元素的基类,虽然它自己什么也不绘制,但是它定义了绘制图元所需要的大部分通用属性,比如 x 、 y 、 width 、 height 、 锚定( anchoring )和按键处理。

在QML中,parent关键字代表着父类

Item 属性 states: list

用于向Item提供必要的属性列表(the list of possible states for this item),如果要改变此Item的state,仅需要将state属性设置为任意一个定义的states,或将其state属性指向一个空串,来回复此Item 的default状态。

Item 属性 PropertyChanges

PropertyChanges用于定义多个属性或直接绑定一个state,这个功能确保当Item在不同states中变换时,其属性只也做对应改变。 注意:当实现一个PropertyChanges对象时,需要指定目标Item,针对指定的item来定义或绑定新属性。

eg:

import QtQuick 2.0

Item {
    id: container
    width: 300; height: 300

    Rectangle {
        id: rect
        width: 100; height: 100
        color: "red"

        MouseArea {
           id: mouseArea
           anchors.fill: parent
        }

        states: State {
           name: "resized"; when: mouseArea.pressed
           PropertyChanges { target: rect; color: "blue"; height: container.height }
        }
    }
}

在这个例子中,当鼠标被点击后,id为rect的Rectangle将会变为蓝色,高变为container的高。

⚠️ **GitHub.com Fallback** ⚠️