查看完整版本: [-- qml 组件创建注意事项 createObject --]

QTCN开发网 -> Qt代码秀 -> qml 组件创建注意事项 createObject [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

happylatch 2015-07-08 10:24

qml 组件创建注意事项 createObject


创建动态组件时候,如果在createObject函数里面没有设定组件的高宽度,则在组件的Component.onCompleted内部
获取的高度宽度是默认的,在组件的单击里面获取的高宽度才是后来设定的88。假如创建组件myW.qml
代码如下:
var componentFT = Qt.createComponent("myW.qml");
            if (componentFT.status === Component.Ready)
            {
                var ftObj = componentFT.createObject(parent, {"folderName":"good name"/*, "width":88, "height":88*/});
        ftObj.width = 88;
                ftObj.height = 88;
            }



如果在createObject函数里面设定组件的高宽度,则在组件的Component.onCompleted内部
获取的高度宽度是就是设定的,代码如下:
Component.onCompleted: {
            var componentFT = Qt.createComponent("myW.qml");
            if (componentFT.status === Component.Ready)
            {
                var ftObj = componentFT.createObject(parent, {"folderName":"good name", "width":88, "height":88});

            }
        }



-------------myW.qml如下----------------
import QtQuick 2.0

Rectangle {
    width: 100    // 默认高度
    height: 62

    Component.onCompleted: console.log("compoent width=",width, "height=",height);
    MouseArea{
        id:idMa
        anchors.fill: parent

        onClicked: {
            console.log("click, width=", width, "height=", height);
        }
    }

}

彩阳 2015-07-09 21:37
对啊。这很正常。不仅仅是宽和高,还有其它属性也是这样的。

qyvlik 2015-08-20 10:13
动态组件创建,多数用于页面传参。页面传参最好在createObject这里设定。

姜小白 2016-04-23 10:11
qyvlik:动态组件创建,多数用于页面传参。页面传参最好在createObject这里设定。
 (2015-08-20 10:13) 

参数获取呢?有没有比信号槽更好的方法?


查看完整版本: [-- qml 组件创建注意事项 createObject --] [-- top --]



Powered by phpwind v8.7 Code ©2003-2011 phpwind
Gzip disabled