Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Access QProperty from C++
Forum Updated to NodeBB v4.3 + New Features

Access QProperty from C++

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • saitejS Offline
    saitejS Offline
    saitej
    wrote on last edited by saitej
    #1

    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*

    1 Reply Last reply
    0
    • RajeeshRaveendranR Offline
      RajeeshRaveendranR Offline
      RajeeshRaveendran
      wrote on last edited by
      #2

      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,
      eg:-

      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.

      Regards,
      Rajeesh Raveendran

      saitejS 1 Reply Last reply
      3
      • RajeeshRaveendranR RajeeshRaveendran

        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,
        eg:-

        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.

        Regards,
        Rajeesh Raveendran

        saitejS Offline
        saitejS Offline
        saitej
        wrote on last edited by
        #3

        @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!

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved