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. Next Noobie question...on C++ / QML integration
QtWS25 Last Chance

Next Noobie question...on C++ / QML integration

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
7 Posts 4 Posters 735 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by SPlatten
    #1

    I have a C++ object that corresponds to an instance of a QML file. I have properties in the C++ class:

        Q_PROPERTY(bool canBeStale READ canBeStale WRITE setCanBeStale)
        Q_PROPERTY(double* dataLink READ pDataLink WRITE setDataLink)
        Q_PROPERTY(QString dataTag READ strUDT WRITE setUDT)
        Q_PROPERTY(double value READ getValue WRITE setValue NOTIFY dataUpdate)
    

    And similar properties in the QML:

        id: root
        readonly property alias api           : datahlpr
        property bool canbeStale              : false
        property var dataLink
        property var dataTag
    

    Is there a way to map the QML and C++ properties as presently I have added to the QML:

        Component.onCompleted: {
        //Set-up mappings to C++ object
            api.dataTag = root.dataTag
            api.dataTag = root.dataTag
            api.canBeStale = root.canbeStale
        }
    

    Kind Regards,
    Sy

    B 1 Reply Last reply
    0
    • SPlattenS SPlatten

      I have a C++ object that corresponds to an instance of a QML file. I have properties in the C++ class:

          Q_PROPERTY(bool canBeStale READ canBeStale WRITE setCanBeStale)
          Q_PROPERTY(double* dataLink READ pDataLink WRITE setDataLink)
          Q_PROPERTY(QString dataTag READ strUDT WRITE setUDT)
          Q_PROPERTY(double value READ getValue WRITE setValue NOTIFY dataUpdate)
      

      And similar properties in the QML:

          id: root
          readonly property alias api           : datahlpr
          property bool canbeStale              : false
          property var dataLink
          property var dataTag
      

      Is there a way to map the QML and C++ properties as presently I have added to the QML:

          Component.onCompleted: {
          //Set-up mappings to C++ object
              api.dataTag = root.dataTag
              api.dataTag = root.dataTag
              api.canBeStale = root.canbeStale
          }
      
      B Offline
      B Offline
      Bob64
      wrote on last edited by
      #2

      @SPlatten I am not sure what you mean by the "corresponding" QML file here. The C++ defines an object that is instantiable in QML directly which already has the properties exposed and so on. When you instantiate it, you will assign the properties with values from your QML environment.

      So for example, say you have exposed MyComponent from C++ with the properties you mentioned.

      Then in some QML component that uses this you will have (e.g.):

      MyClientComponent.qml

      Item {
          ...
          MyComponent {
              id: mycomp
              canbeStale: true
              dataTag: "bob"
              ...
      

      You can bind to and from the MyComponent properties in the client QML component in the usual way.

      SPlattenS 1 Reply Last reply
      0
      • B Bob64

        @SPlatten I am not sure what you mean by the "corresponding" QML file here. The C++ defines an object that is instantiable in QML directly which already has the properties exposed and so on. When you instantiate it, you will assign the properties with values from your QML environment.

        So for example, say you have exposed MyComponent from C++ with the properties you mentioned.

        Then in some QML component that uses this you will have (e.g.):

        MyClientComponent.qml

        Item {
            ...
            MyComponent {
                id: mycomp
                canbeStale: true
                dataTag: "bob"
                ...
        

        You can bind to and from the MyComponent properties in the client QML component in the usual way.

        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by
        #3

        @Bob64 , what I was really asking for was is there a way to auto assign properties of the same name in the QML object to the C++ equivalent object ?

        Kind Regards,
        Sy

        B 1 Reply Last reply
        0
        • SPlattenS SPlatten

          @Bob64 , what I was really asking for was is there a way to auto assign properties of the same name in the QML object to the C++ equivalent object ?

          B Offline
          B Offline
          Bob64
          wrote on last edited by
          #4

          @SPlatten I think the issue that I still don't understand what you mean by "equivalent" object. Can you give a minimal example that shows more of the structure of your real system? The code in your original post just showed properties so it was difficult to see what components/classes were involved and how they related to each other.

          1 Reply Last reply
          0
          • JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by
            #5

            Does binding help? Check the small example here.
            https://stackoverflow.com/questions/41232999/two-way-binding-c-model-in-qml

            SPlattenS 1 Reply Last reply
            0
            • JoeCFDJ JoeCFD

              Does binding help? Check the small example here.
              https://stackoverflow.com/questions/41232999/two-way-binding-c-model-in-qml

              SPlattenS Offline
              SPlattenS Offline
              SPlatten
              wrote on last edited by
              #6

              @JoeCFD , yes, it would, however, the main reason I was asking was if there was already a very simple and transparent method of doing the same.

              Kind Regards,
              Sy

              1 Reply Last reply
              0
              • L Offline
                L Offline
                lemons
                wrote on last edited by
                #7

                Maybe my simple example I posted earlier this year helps you:
                https://forum.qt.io/topic/135077/creating-a-c-identifier-for-a-dynamically-generated-object/2

                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