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++ to Qml with 'sub objects'
QtWS25 Last Chance

C++ to Qml with 'sub objects'

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 3 Posters 761 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.
  • C Offline
    C Offline
    Cyrille de Brebisson
    wrote on last edited by Cyrille de Brebisson
    #1

    Hello,

    I want to have a first Qml exposed class (Object2) which has a property which is itself a Qml exposed object of class Object1.

    in pseudo code, assuming myOb contains Object2, I want to be able to do: myOb.ob1.value, I also want to be able to do property binding with myOb.ob1.value

    I did as follow, but it does not work (C++ compilation errors, complaining about "= operators" in the moc files. What is the proper way to do such things?
    Thanks

    // The "sub object". It is a very "normal" C++ to Qml interface object with just one property (value).
    class Object1 : public QObject {
    Q_OBJECT
    Q_PROPERTY(int value READ getValue WRITE setValue NOTIFY valueChanged)
    Q_SIGNALS:
    void valueChanged(int value);
    public:
    Object1(QObject *parent=nullptr): QObject(parent), _value(0) { }
    int _value;
    int getValue() { return _value; }
    void setValue(int v) { if (v==_value) return; _value= v; emit (valueChanged(v)); }
    };

    // The top level object. It has one read only property, which is an instance of Object1
    class Object2 : public QObject {
    Q_OBJECT
    Q_PROPERTY(Object1 obj1 READ getObj1)
    Q_SIGNALS:
    public:
    Object1 _obj1;
    Object1 &getObj1() { return _obj1; }
    Object2(QObject *parent=nullptr): QObject(parent), _obj1(this) { }
    }

    J.HilkJ 1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Inner object is also QObject. So copy constructor and assignment operator are disabled. Set and get function in outer object use copy and assignment operator. Hence you are seeing this issue. You can try declaring the inner object as pointer. It should work.

      class MyOuter : public QObject
      {
      Q_OBJECT
      Q_PROPERTY(InnerElement *myelem READ getMy_elm WRITE setMy_elm NOTIFY myelemChanged)

      public:
      explicit MyOuter(QObject *parent = nullptr);

      InnerElement *getMy_elm() const;
      void setMy_elm(InnerElement *value);
      

      signals:
      void nameChanged();
      void myelemChanged();

      public slots:

      private :
      InnerElement *my_elm;
      };

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      2
      • C Cyrille de Brebisson

        Hello,

        I want to have a first Qml exposed class (Object2) which has a property which is itself a Qml exposed object of class Object1.

        in pseudo code, assuming myOb contains Object2, I want to be able to do: myOb.ob1.value, I also want to be able to do property binding with myOb.ob1.value

        I did as follow, but it does not work (C++ compilation errors, complaining about "= operators" in the moc files. What is the proper way to do such things?
        Thanks

        // The "sub object". It is a very "normal" C++ to Qml interface object with just one property (value).
        class Object1 : public QObject {
        Q_OBJECT
        Q_PROPERTY(int value READ getValue WRITE setValue NOTIFY valueChanged)
        Q_SIGNALS:
        void valueChanged(int value);
        public:
        Object1(QObject *parent=nullptr): QObject(parent), _value(0) { }
        int _value;
        int getValue() { return _value; }
        void setValue(int v) { if (v==_value) return; _value= v; emit (valueChanged(v)); }
        };

        // The top level object. It has one read only property, which is an instance of Object1
        class Object2 : public QObject {
        Q_OBJECT
        Q_PROPERTY(Object1 obj1 READ getObj1)
        Q_SIGNALS:
        public:
        Object1 _obj1;
        Object1 &getObj1() { return _obj1; }
        Object2(QObject *parent=nullptr): QObject(parent), _obj1(this) { }
        }

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @Cyrille-de-Brebisson addionally to what @dheerendra said,

        read this section Properties with Object Types from the docu.

        Also, because I don't see you registering your class anywhere in the example, read up on http://doc.qt.io/qt-5/qtqml-cppintegration-definetypes.html#registering-c-types-with-the-qml-type-system or you won't be able to use your custom class/object.


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        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