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++ class members from qml
QtWS25 Last Chance

C++ class members from qml

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 851 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.
  • ODБOïO Offline
    ODБOïO Offline
    ODБOï
    wrote on last edited by
    #1

    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
    0
    • M medyakovvit

      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ïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by
      #3

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

      1 Reply Last reply
      0
      • M Offline
        M Offline
        medyakovvit
        wrote on last edited by
        #2

        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ïO 1 Reply Last reply
        2
        • M medyakovvit

          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ïO Offline
          ODБOïO Offline
          ODБOï
          wrote on last edited by
          #3

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

          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