Home > Back-end >  Create a Q_PROPERTY to a QObject which has it's own Q_PROPERTY's
Create a Q_PROPERTY to a QObject which has it's own Q_PROPERTY's

Time:03-05

I have an QInnerItem with two Q_PROPERTIES

class QInnerItem : public QObject {
    Q_OBJECT
    Q_PROPERTY(int bar1 READ bar1 WRITE setBar1 NOTIFY bar1Changed)
    Q_PROPERTY(int bar2 READ bar2 WRITE setBar2 NOTIFY bar2Changed)

public:

    QInnerItem(QObject* owner) : QObject(owner) {}

    void setBar1(const int& bar1) {
        m_bar1 = bar1;
        emit bar1Changed();
    }
    int bar1() const {
        return m_bar1;
    }
    void setBar2(const int& bar2) {
        m_bar2 = bar2;
        emit bar2Changed();
    }
    int bar2() const {
        return m_bar2;
    }
signals:
    void bar1Changed();
    void bar2Changed();
private:
    int m_bar1;
    int m_bar2;
};

And a QOuterItem that is composed with a QInnerItem and an int.

class QOuterItem : public QObject {
    Q_OBJECT
    Q_PROPERTY(int foo READ foo WRITE setFoo NOTIFY fooChanged)
    Q_PROPERTY(QInnerItem bar READ bar)
public:
    void setFoo(const int& foo) {
        m_foo = foo;
        emit fooChanged();
    }
    int foo() const {
        return m_foo;
    }
    const QInnerItem& bar() const { //must return either the property's type or a const reference to that type
        return m_bar;
    }
signals:
    void fooChanged();
private:
    int m_foo;
    QInnerItem m_bar;
};

This gives me the error:

moc_Model.cpp:264: error: C2280: 'QInnerItem &QInnerItem::operator =(const QInnerItem &)': attempting to reference a deleted function

I believe this is because a QObject has an explicitly deleted copy assignment operator: https://doc.qt.io/qt-5/qobject.html#no-copy-constructor-or-assignment-operator

Is there any way to access a reference to m_bar without the copy assignment operator?

From the same link, it also says:

"The main consequence is that you should use pointers to QObject (or to your QObject subclass) where you might otherwise be tempted to use your QObject subclass as a value. For example, without a copy constructor, you can't use a subclass of QObject as the value to be stored in one of the container classes. You must store pointers."

So I have tried:

class QOuterItem : public QObject {
    Q_OBJECT
    Q_PROPERTY(int foo READ foo WRITE setFoo NOTIFY fooChanged)
    Q_PROPERTY(QInnerItem bar READ bar)
public:
    void setFoo(const int& foo) {
        m_foo = foo;
        emit fooChanged();
    }
    int foo() const {
        return m_foo;
    }
    const QInnerItem& bar() const { //must return either the property's type or a const reference to that type
        return *m_bar;
    }
signals:
    void fooChanged();
private:
    int m_foo;
    QInnerItem* m_bar = new QInnerItem(this);
};

But this gives the exact same error.

How can I achieve what I am trying to do? i.e: Have a QObject with properties, and one of those properties is a QObject with it's own properties.

CodePudding user response:

You were misunderstanding what you quoted: "you should use pointers to QObject". It doesn't matter if the actual member variable is a pointer or not. What matters is how the Q_PROPERTY is accessed. So you can do something like this:

class QOuterItem : public QObject {
    Q_OBJECT
    Q_PROPERTY(QInnerItem *bar READ bar)
public:
    QInnerItem *bar() const {
        return &m_bar;
    }
private:
    QInnerItem m_bar;
};
  • Related