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
Forum Update on Monday, May 27th 2025

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

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
7 Posts 4 Posters 747 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.
  • S Offline
    S Offline
    SPlatten
    wrote on 1 Dec 2022, 11:14 last edited by SPlatten 12 Jan 2022, 11:15
    #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 1 Dec 2022, 12:38
    0
    • S SPlatten
      1 Dec 2022, 11:14

      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 1 Dec 2022, 12:38 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.

      S 1 Reply Last reply 1 Dec 2022, 13:25
      0
      • B Bob64
        1 Dec 2022, 12:38

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

        S Offline
        S Offline
        SPlatten
        wrote on 1 Dec 2022, 13:25 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 1 Dec 2022, 14:17
        0
        • S SPlatten
          1 Dec 2022, 13:25

          @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 1 Dec 2022, 14:17 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
          • J Offline
            J Offline
            JoeCFD
            wrote on 1 Dec 2022, 14:45 last edited by
            #5

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

            S 1 Reply Last reply 1 Dec 2022, 14:47
            0
            • J JoeCFD
              1 Dec 2022, 14:45

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

              S Offline
              S Offline
              SPlatten
              wrote on 1 Dec 2022, 14:47 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 1 Dec 2022, 15:00 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

                1/7

                1 Dec 2022, 11:14

                • Login

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