code snippet

QML:

动态创建对象

import QtQuick 1.0

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

     function loadButton() {
         var component = Qt.createComponent("Button.qml");
         if (component.status == Component.Ready) {
             var button = component.createObject(container);
             button.color = "red";
         }
     }

     Component.onCompleted: loadButton()
 }

动态删除对象(不要试图手动删除由Repeater或者Loader创建的实例)

application.qml SelfDestroyingRect.qml
 import QtQuick 1.0

 Item {
     id: container
     width: 500; height: 100

     Component.onCompleted: {
         var component = Qt.createComponent("SelfDestroyingRect.qml");
         for (var i=0; i<5; i++) {
             var object = component.createObject(container);
             object.x = (object.width + 10) * i;
         }
     }
 }
 import QtQuick 1.0

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

     NumberAnimation on opacity {
         to: 0
         duration: 1000

         onRunningChanged: {
             if (!running) {
                 console.log("Destroying...")
                 rect.destroy();
             }
         }
     }
 }
从状态变换时的过渡


ImageDisplay
    {
        id: imageDisplay
        width: 0; height: parent.height;
        anchors.left: catalog.right;
        visible: false;

        states: [
            State {
                name: "display"; when: visiable===true;
                PropertyChanges {
                    target: imageDisplay
                    width: root.width - catalog.width;
                }
            }
        ]
        transitions: [
            Transition {
                SpringAnimation {
                    target: imageDisplay;
                    property: "width";
                    damping: 0.2;
                    spring: 2;
                }
            }
        ]

    }



手工切换元素状态:

Rectangle {

id: root;

states: [
        State {
            name: "minSize"
            PropertyChanges {
                target: hideButtonLabel
                text: ">"
            }
            PropertyChanges {
                target: root
                width: 10;
            }
        }
    ]
    transitions: [
        Transition {
            NumberAnimation { target: root; property: "width"; duration: 200; easing.type: Easing.InOutQuad }
        }
    ]


        MouseArea {
            anchors.fill: parent;
            onClicked: {
                if(root.state == '') {
                    root.state = "minSize"
                } else {
                    root.state = ''
                }
            }
        }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值