如何在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