Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved C++ class members from qml

    QML and Qt Quick
    2
    3
    688
    Loading More Posts
    • 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.
    • ODБOï
      ODБOï last edited by

      Hello !
      I want to read Q_PROPERTY of a member object, and display it in qml.

      I have a class A witch has a private member object from class B like this :
      class A
      {
      //Q_PROPERTY QString localS = "localOK" this will work in qml : a.getLocalS
      private:
      B *b // class B has Q_PROPERTY QString testStr = "test" and getTestStr() getter
      }
      In main.cpp, i set an instance of class 'A' in contextProperties
      A a;
      engine.rootContext()->setContextProperty("a",&a);

      Now in main.qml i want to read b objects testStr

      Text{
      //text : a.getLocalS this is OK
      text:a.b.getTestStr // this will not work
      }

      How can i access b by a ?

      Is it possible to create Q_Property in A, witch returns my b ?
      Q_PROPERTY(B myB READ getB )
      *B getB(){
      return b;
      }

      and then do :

      Text{
      text:a.getB.getTestStr
      }

      thank you in advance
      Levon

      1 Reply Last reply Reply Quote 0
      • ODБOï
        ODБOï @medyakovvit last edited by

        @medyakovvit Thx very much this is exactly what i was reading now ! :)
        I will try this tomorrow

        1 Reply Last reply Reply Quote 0
        • M
          medyakovvit last edited by

          Hi, @LeLev
          From Qt's doc:

          if the class itself is to be used as a method parameter or property, or if one of its enum types is to be used in this way — then the class may need to be registered.

          Registering C++ Types with the QML Type System

          ODБOï 1 Reply Last reply Reply Quote 2
          • ODБOï
            ODБOï @medyakovvit last edited by

            @medyakovvit Thx very much this is exactly what i was reading now ! :)
            I will try this tomorrow

            1 Reply Last reply Reply Quote 0
            • First post
              Last post