Question on QDeclarativeItem attribute/properties



  • See this code snip,

    @
    Text {
    id: text2
    text: qsTr("Hello devnet")
    }
    @

    If I now try to obtain the properties of this via this code:

    @
    QDeclarativeItem childitem = qobject_cast<QDeclarativeItem>(myobject);
    const QMetaObject *metaObject = childitem->metaObject();
    for(int j = 0; j < metaObject->propertyCount(); ++j)
    @

    and in the loop if I display all the properties, I find 2 issues that I'm seeking an answer to ...

    1. How can I get the id, "text2"
    2. The property "text" just shows "Hello devnet", how can I know if qsTr was used or not


  • My understanding is that in QML the id and objectName are separate and in your case you might be able to access it via objectName.

    You can explicitly give an objectName like this

    @Text {
    id: text2
    objectName: "objText2"
    }@



  • Any property can be bound to arbitrary JavaScript. Are you asking to get the binding data or perhaps the source code for the binding?



  • [quote author="chetankjain" date="1292173184"]1. How can I get the id, "text2"[/quote]

    The id is not currently accessible from C++ (it is not an actual property in the meta object system). As QtK pointed out, you can use objectName, though generally it is discouraged to work with the internals of a QML app from C++ in this way (see the warning "here":http://doc.qt.nokia.com/4.7-snapshot/qtbinding.html#locating-child-objects)

    [quote author="chetankjain" date="1292173184"]2. The property "text" just shows "Hello devnet", how can I know if qsTr was used or not[/quote]

    This is also not currently possible to get from the meta object system (which only gives access to the value of the property), unless the property is of type "QDeclarativeScriptString":http://doc.qt.nokia.com/4.7-snapshot/qdeclarativescriptstring.html.

    Can you say any more about what you would like to use these two pieces of information for? Note that the (private) QML debug/dom API allows access to both of these things for Creator.

    Regards,
    Michael



  • [quote author="QtK" date="1292175104"]My understanding is that in QML the id and objectName are separate and in your case you might be able to access it via objectName.
    [/quote]
    No I do not want to use the objectName, I will explain my scenario below ...



  • [quote author="mbrasser" date="1292200382"]though generally it is discouraged to work with the internals of a QML app from C++ in this way (see the warning "here":http://doc.qt.nokia.com/4.7-snapshot/qtbinding.html#locating-child-objects)
    ...

    This is also not currently possible to get from the meta object system (which only gives access to the value of the property), unless the property is of type "QDeclarativeScriptString":http://doc.qt.nokia.com/4.7-snapshot/qdeclarativescriptstring.html.

    Can you say any more about what you would like to use these two pieces of information for? Note that the (private) QML debug/dom API allows access to both of these things for Creator.
    [/quote]

    I'm writing a test application to do some specific tests/checks on a QML based application and wanted this info from the QML object tree/composition .. my access is read only and will never modify the QML.

    Thanks for the links. :)
    I will chk further and revert ...



  • [quote author="chetankjain" date="1292307467"]I'm writing a test application to do some specific tests/checks on a QML based application and wanted this info from the QML object tree/composition .. my access is read only and will never modify the QML.[/quote]

    For reference, here is an earlier discussion on the mailing list regarding testing/id access:
    http://lists.trolltech.com/pipermail/qt-qml/2010-November/001532.html

    Regards,
    Michael



  • thanks Micheal, this is very helpful, :)
    I'll explore qmlunit next, probably relook at my approach



  • One more link of interest in regards to testing QML:
    http://bugreports.qt.nokia.com/browse/QTBUG-15801 (QML testing requirements)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.