Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Is it safe to bind to C++ subobject's property in QML?
QtWS25 Last Chance

Is it safe to bind to C++ subobject's property in QML?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qmlc++propertiesbinding
4 Posts 2 Posters 590 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.
  • B Offline
    B Offline
    b-arun-kumar
    wrote on 7 Dec 2021, 15:27 last edited by
    #1

    C++:
    database c++ object is exposed to QML as a context property.
    database c++ object has a method getDbpointObject() that returns pointer to databasePoint C++ object.
    databasePoint C++ object has a property named cppProp.

    main.cpp:

    // expose database object to qml
    database databaseObj;
    engine.rootContext()->setContextProperty("database", (QObject*)&databaseObj);
    // register databasePoint class
    qmlRegisterType<databasePoint>("DBPoint", 1, 0, "DBPoint");
    

    database.h:

    databasePoint *database::getDbpointObject()
    

    databasePoint.h:

    Q_PROPERTY(QVariant cppProp READ cppProp WRITE setcppProp NOTIFY cppPropChanged)
    

    QML:
    qmlComp is a custom QML component.
    qmlComp has a QML property named qmlCompProp.
    On completion of qmlComp creation, databasePoint c++ object is assigned to qmlCompProp.

    qmlComp.qml:

    Item 
    {
    property var qmlCompProp: ({})   // qml property
    Component.onCompleted:
    	{
    		qmlCompProp = database.getDbpointObject() // qml property holds the databasePoint c++ object
    	}		
    }
    

    Question:
    In binding.qml, QML property bindProp is binded to myQmlComp.qmlCompProp.cppProp
    Is this binding safe?
    Will the binding always be resolved?
    databasePoint c++ object is assigned to qmlCompProp in Component.onCompleted. Until then, qmlCompProp is an empty object. Will it have an impact on binding resolution?
    Will the order of properties evaluation in binding.qml have an impact on binding resolution?

    binding.qml:

    property int bindProp: myQmlComp.qmlCompProp.cppProp // is this binding safe?
    qmlComp{id: myQmlComp}
    
    B 1 Reply Last reply 9 Dec 2021, 12:41
    0
    • B b-arun-kumar
      7 Dec 2021, 15:27

      C++:
      database c++ object is exposed to QML as a context property.
      database c++ object has a method getDbpointObject() that returns pointer to databasePoint C++ object.
      databasePoint C++ object has a property named cppProp.

      main.cpp:

      // expose database object to qml
      database databaseObj;
      engine.rootContext()->setContextProperty("database", (QObject*)&databaseObj);
      // register databasePoint class
      qmlRegisterType<databasePoint>("DBPoint", 1, 0, "DBPoint");
      

      database.h:

      databasePoint *database::getDbpointObject()
      

      databasePoint.h:

      Q_PROPERTY(QVariant cppProp READ cppProp WRITE setcppProp NOTIFY cppPropChanged)
      

      QML:
      qmlComp is a custom QML component.
      qmlComp has a QML property named qmlCompProp.
      On completion of qmlComp creation, databasePoint c++ object is assigned to qmlCompProp.

      qmlComp.qml:

      Item 
      {
      property var qmlCompProp: ({})   // qml property
      Component.onCompleted:
      	{
      		qmlCompProp = database.getDbpointObject() // qml property holds the databasePoint c++ object
      	}		
      }
      

      Question:
      In binding.qml, QML property bindProp is binded to myQmlComp.qmlCompProp.cppProp
      Is this binding safe?
      Will the binding always be resolved?
      databasePoint c++ object is assigned to qmlCompProp in Component.onCompleted. Until then, qmlCompProp is an empty object. Will it have an impact on binding resolution?
      Will the order of properties evaluation in binding.qml have an impact on binding resolution?

      binding.qml:

      property int bindProp: myQmlComp.qmlCompProp.cppProp // is this binding safe?
      qmlComp{id: myQmlComp}
      
      B Offline
      B Offline
      b-arun-kumar
      wrote on 9 Dec 2021, 12:41 last edited by
      #2

      Response from Qt Support:

      In binding.qml, QML property bindProp is binded to myQmlComp.qmlCompProp.cppProp Is this binding safe?
      Looks like.

      Will the binding always be resolved?
      Assuming no reference issues, yes

      databasePoint c++ object is assigned to qmlCompProp in Component.onCompleted. Until then, qmlCompProp is an empty object. Will it have an impact on binding resolution?
      This case should work. However, the binding is initially made for the empty object in that case (depending on how this is all instantiated) and then after the onCompleted, it updates the binding to refer to that real cppProp (instead of dummy one it created to the empty object).

      Will the order of properties evaluation in binding.qml have an impact on binding resolution?
      Not in this case at least. You could have some issue if you had other dependent properties for the binding, like if you had an array in between and its index was another property.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        GrecKo
        Qt Champions 2018
        wrote on 9 Dec 2021, 13:28 last edited by
        #3

        It's safe as in it won't crash.

        Before your onCompleted the property int bindProp: myQmlComp.qmlCompProp.cppProp will give a warning about binding undefined to int.

        You could bypass that by doing property int bindProp: myQmlComp.qmlCompProp.cppProp ?? 0 //nullish coalescing

        But why don't you do property var qmlCompProp: database.getDbpointObject() in yout qmlComp?

        B 1 Reply Last reply 10 Dec 2021, 05:47
        0
        • G GrecKo
          9 Dec 2021, 13:28

          It's safe as in it won't crash.

          Before your onCompleted the property int bindProp: myQmlComp.qmlCompProp.cppProp will give a warning about binding undefined to int.

          You could bypass that by doing property int bindProp: myQmlComp.qmlCompProp.cppProp ?? 0 //nullish coalescing

          But why don't you do property var qmlCompProp: database.getDbpointObject() in yout qmlComp?

          B Offline
          B Offline
          b-arun-kumar
          wrote on 10 Dec 2021, 05:47 last edited by b-arun-kumar
          #4

          @GrecKo said in Is it safe to bind to C++ subobject's property in QML?:

          But why don't you do property var qmlCompProp: database.getDbpointObject() in yout qmlComp?

          prop1 and prop2 are passed to the database.getDbpointObject(prop1, prop2) function. prop1 and prop2 are properties of qmlComp. These properties are set when an instance of qmlComp is created.

          Item 
          {
          required property string prop1
          property bool prop2: false
          property var qmlCompProp: ({})
          Component.onCompleted:
          	{
          		qmlCompProp = database.getDbpointObject(prop1, prop2)
          	}		
          }
          

          Since the properties evaluation order is undefined, and qmlCompProp depends on both prop1 and prop2 values, I've opted to initialize qmlCompProp with an empty object and assign the actual object in Component.onCompleted.
          Is this the right/recommended way, or is there a better way to handle this?

          Thanks for the information about the warning and it's workaround.

          1 Reply Last reply
          0

          4/4

          10 Dec 2021, 05:47

          • Login

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