C++ to Qml with 'sub objects'
-
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) { }
} -
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;
}; -
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) { }
}@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.