Important: Please read the Qt Code of Conduct -

Access QProperty from C++

  • Hi all,

    I am trying to access a property from C++ in QML but I get this warning:

    "Unable to assign [undefined] to double value".

    I have defined the property in a class : Mitem as:

        Q_PROPERTY(qreal  area MEMBER _area NOTIFY areaUpdated)

    I have registered this class using qmlRegisterType

    I have tried importing in QML as:

        property real iArea: Mitem.area

    I have checked online and I have come across Binding and I tried that out but it gives a different error:

        Binding{target:Mitem property: "area";value: iArea }

    error: Unable to assign undefined to QObject*

  • Hi,

    It seems you have done the property definition correctly. But not the correct way you are accessing it.
    qmlRegisterType() API registers your class so that "Mitem" can be used similar as any other QML components (say "Rectangle").

    I dont know the propose of your code, but I can say the possible usage (2 ways) of your property.

    1. Using qmlRegisterType()

    After registering your class using this API, you can create objects of this class in QML similar like QML components,

    Mitem { /// instantiating your class object
    id: mItem

    TextInput {
    id: texInput
    text: mItem.area /// accessing your property.

    1. using QQmlContext::setConextProperty()

    using this API you can set the class object as a property that can access your object property throught your QML code.

    eg: - ///// in C++ code

    Mitem* pItem = new Mitem();

    pContext->setConextProperty( "MyPropertyClass", pItem); /// pContext is your application QML context.

    //// in QML code

    Text {
    text: MyPropertyClass.area /// access your object property with the name u specified in setConextProperty() API

    Hope this will help you.

    Rajeesh Raveendran

  • @RajeeshRaveendran

    Thanks!! I have used the QQmlContext::setConextProperty() but the thing was there are many properties to set in a similar manner. So I wanted to try this way. Got a better picture now!

Log in to reply