如何在QML應用中得到一個Item的所有屬性,訊號及方法,qmlitem

來源:互聯網
上載者:User

如何在QML應用中得到一個Item的所有屬性,訊號及方法,qmlitem

Item是QML語言中最基本的元素。有時為了方便,我們可以列出它裡面的所有的屬性,訊號及方法。我們可以通過這個方法來修改我們的屬性等。在QML語言中,所有的可視的控制項都是繼承於Item的。


下面我們來通過一個例子來展示如何這麼做。我們可以設計一個簡單的QML應用如下:


import QtQuick 2.0import Ubuntu.Components 1.1/*!    \brief MainView with a Label and Button elements.*/MainView {    // objectName for functional testing purposes (autopilot-qt5)    objectName: "mainView"    // Note! applicationName needs to match the "name" field of the click manifest    applicationName: "properties.liu-xiao-guo"    /*     This property enables the application to change orientation     when the device is rotated. The default is false.    */    //automaticOrientation: true    // Removes the old toolbar and enables new features of the new header.    useDeprecatedToolbar: false    width: units.gu(100)    height: units.gu(75)    Page {        title: i18n.tr("properties")        Rectangle {            id: rect            x: 0; y: 0            width: 100; height: 100            color: "blue"            Component.onCompleted: {                var keys = Object.keys(rect);                for(var i = 0; i < keys.length; i++) {                    var key = keys[i];                    // prints all properties, signals, functions from object                    console.log(key + ' : ' + rect[key]);                    if (key === "x") {                        rect[key] = 100;                    }                }            }        }    }}


這裡rect最初的座標為(0,0)。在Component.onCompleted中,我們遍曆所有的屬性,訊號及方法。我們把x的值修改為100。最後啟動並執行結果如下:



在Qt Creator的“Application Output”視窗中,我們可以看到:


qml: objectName : qml: parent : Page11_QMLTYPE_42(0x1a55340)qml: data : [object Object]qml: resources : [object Object]qml: children : [object Object]qml: x : 0qml: y : 0qml: z : 0qml: width : 100qml: height : 100qml: opacity : 1qml: enabled : trueqml: visible : trueqml: visibleChildren : [object Object]qml: states : [object Object]qml: transitions : [object Object]qml: state : qml: childrenRect : QRectF(0, 0, 0, 0)qml: anchors : QQuickAnchors(0x1a49840)qml: left : QVariant(QQuickAnchorLine)qml: right : QVariant(QQuickAnchorLine)qml: horizontalCenter : QVariant(QQuickAnchorLine)qml: top : QVariant(QQuickAnchorLine)qml: bottom : QVariant(QQuickAnchorLine)qml: verticalCenter : QVariant(QQuickAnchorLine)qml: baseline : QVariant(QQuickAnchorLine)qml: baselineOffset : 0qml: clip : falseqml: focus : falseqml: activeFocus : falseqml: activeFocusOnTab : falseqml: rotation : 0qml: scale : 1qml: transformOrigin : 4qml: transformOriginPoint : QPointF(50, 50)qml: transform : [object Object]qml: smooth : trueqml: antialiasing : falseqml: implicitWidth : 0qml: implicitHeight : 0qml: layer : QQuickItemLayer(0x1b90010)qml: color : #0000ffqml: gradient : nullqml: border : QQuickPen(0x1b8bd50)qml: radius : 0qml: objectNameChanged : function() { [code] }qml: childrenRectChanged : function() { [code] }qml: baselineOffsetChanged : function() { [code] }qml: stateChanged : function() { [code] }qml: focusChanged : function() { [code] }qml: activeFocusChanged : function() { [code] }qml: activeFocusOnTabChanged : function() { [code] }qml: parentChanged : function() { [code] }qml: transformOriginChanged : function() { [code] }qml: smoothChanged : function() { [code] }qml: antialiasingChanged : function() { [code] }qml: clipChanged : function() { [code] }qml: windowChanged : function() { [code] }qml: childrenChanged : function() { [code] }qml: opacityChanged : function() { [code] }qml: enabledChanged : function() { [code] }qml: visibleChanged : function() { [code] }qml: visibleChildrenChanged : function() { [code] }qml: rotationChanged : function() { [code] }qml: scaleChanged : function() { [code] }qml: xChanged : function() { [code] }qml: yChanged : function() { [code] }qml: widthChanged : function() { [code] }qml: heightChanged : function() { [code] }qml: zChanged : function() { [code] }qml: implicitWidthChanged : function() { [code] }qml: implicitHeightChanged : function() { [code] }qml: update : function() { [code] }qml: grabToImage : function() { [code] }qml: grabToImage : function() { [code] }qml: contains : function() { [code] }qml: mapFromItem : function() { [code] }qml: mapToItem : function() { [code] }qml: forceActiveFocus : function() { [code] }qml: forceActiveFocus : function() { [code] }qml: nextItemInFocusChain : function() { [code] }qml: nextItemInFocusChain : function() { [code] }qml: childAt : function() { [code] }qml: colorChanged : function() { [code] }qml: radiusChanged : function() { [code] }

這些都是我們可以用到的。通過這個方法,我們可以全面地瞭解rect的所有屬性。特別適用於一些動態生產的控制項。我們可以用來修改它們的一些屬性等。

項目的源碼在:git clone https://gitcafe.com/ubuntu/properties.git

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.