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. C++ Communication with QML

C++ Communication with QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 263 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
    serkan_tr
    wrote on 20 Jan 2023, 06:39 last edited by
    #1

    I'm trying to get the QML side to communicate with C++ in a project. I have a class in my project and this class is called RosQml and inside this class there is a function called getButton().
    I tried two ways and both times I get the same error.
    c++
    1-> qmlRegisterType<RosQml> ("ROSNODE", 1, 0, "Rosnode");
    2-> qmlRegisterUncreatableType<RosQml> ("ROSNODE", 1, 0, "Rosnode", "Reference only");
    QML

    onClicked: {
        Rosnode.getButton()
    }
    

    same error in both cases
    error = TypeError: Property 'getButton' of object [object Object] is not a function

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 20 Jan 2023, 07:41 last edited by
      #2

      By registering a type, you just make it available to the QML engine. It allows you to do this in QML:

      import ROSNODE 1.0
      
      Rosnode {
        // An instance of C++ class, created in QML!
      }
      

      If you want to create an instance of your class in C++ and use that same instance in QML ("share" the same object between QML and C++), then you need to use different approach. For example:

      • add your object to root context property of your engine
      • declare your object to be a QML singleton
      • send the object to QML via a signal
      • there are probably a few more ways

      (Z(:^

      S 1 Reply Last reply 23 Jan 2023, 05:03
      2
      • S sierdzio
        20 Jan 2023, 07:41

        By registering a type, you just make it available to the QML engine. It allows you to do this in QML:

        import ROSNODE 1.0
        
        Rosnode {
          // An instance of C++ class, created in QML!
        }
        

        If you want to create an instance of your class in C++ and use that same instance in QML ("share" the same object between QML and C++), then you need to use different approach. For example:

        • add your object to root context property of your engine
        • declare your object to be a QML singleton
        • send the object to QML via a signal
        • there are probably a few more ways
        S Offline
        S Offline
        serkan_tr
        wrote on 23 Jan 2023, 05:03 last edited by
        #3

        @sierdzio this worked

        1 Reply Last reply
        0

        3/3

        23 Jan 2023, 05:03

        • Login

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