0

I have this class that do not derive from QObject because I need it to be copyable:

#ifndef ITEM_HPP
#define ITEM_HPP

#include <QObject>

class Item
{
  Q_GADGET
  Q_PROPERTY(int intValue READ intValue WRITE setIntValue)
  Q_PROPERTY(QString stringValue READ stringValue WRITE setStringValue)

  public:
    Item();

    int intValue() const { return m_intValue; }
    void setIntValue(int intValue) { m_intValue = intValue; }

    const QString& stringValue() const { return m_stringValue; }
    void setStringValue(const QString& stringValue) { m_stringValue = stringValue; }

  private:
    int m_intValue;
    QString m_stringValue;
};

Q_DECLARE_METATYPE(Item)

#endif // ITEM_HPP

How can I notify the QML when intValue or stringValue changes?
If the class derived from QObject I could add the NOTIFY property, but how can I do this without deriving from QObject?

Fausto01
  • 171
  • 14
  • Can you elaborate on the use-case for "copyable"? – Amfasis Mar 14 '22 at 07:57
  • I've tried to use QObject derived class to expose properties, but I have some problems with memory management of these objects. This is my problem: https://stackoverflow.com/questions/71436798/qt-when-to-delete-qobject-q-property?noredirect=1 – Fausto01 Mar 14 '22 at 09:53
  • I was aware of that question :-) But can you elaborate what you want to achieve in the long run and what constraints you have? – Amfasis Mar 14 '22 at 10:29
  • Sure :) I need to have a class(call it Item) that exposes some properties, then, i need another class(call it Proxy) that exposes a QList as property. If i use shared pointers I have some problems with memory management, so, I was trying to do this without pointers, but Q_GADGET does not have the signal for data change. I hope I have explained it well – Fausto01 Mar 14 '22 at 10:58
  • 1
    you could consider using a QAbstractListModel (that would be the proxy). Takes a bit of effort to set up, but then you can easily add roles – Amfasis Mar 14 '22 at 12:03

0 Answers0