0

I have a set of classes ClassA, ClassB, and ClassC:

class ClassA : public QObject
{
    Q_PROPERTY(int propertyA READ propertyA WRITE setPropertyA NOTIFY propertyAChanged)
}

class ClassB : public QObject
{
    Q_PROPERTY(int propertyB READ propertyB WRITE setPropertyB NOTIFY propertyBChanged)
}

class ClassC : public ClassA, public ClassB //problem: both classes are derived from QObject
{   
    Q_PROPERTY(int propertyC READ propertyC WRITE setPropertyC NOTIFY propertyCChanged)
}

Each class has properties which must be accessible in Qml which means each class has to implement the QObject class. What is the best way to implement this?

Regards

Hyndrix
  • 4,282
  • 7
  • 41
  • 82
  • [Virtual inheritance](https://en.wikipedia.org/wiki/Virtual_inheritance)? – folibis Jan 02 '20 at 10:02
  • 1
    Afaik virtual inheritance does not work with moc/QObject's signaling. – Hyndrix Jan 02 '20 at 10:10
  • 1
    Ah, you are right, so there is no another option except to review the application's class structure to avoid the diamond inheritance. Actually, I would avoid this in all projects because of the ambiguity. – folibis Jan 02 '20 at 11:02
  • I agree. The current state of the code is that interfaces/base classes are kept QObject-free as much as possible. But this creates redundancy because property and signalling has to be defined in each child class. Code maintainability and readability suffers from this a lot. – Hyndrix Jan 02 '20 at 13:15

1 Answers1

0

Make a system for accessing those properties by making methods with Q_INVOKABLE in front of them instead of using Q_PROPERTY by simply creating an instance of ClassA and ClassB inside of ClassC then re-implement the properties to access those instances

class ClassC : public QObject //problem: both classes are derived from 
{   
    Q_PROPERTY(int propertyA READ propertyA WRITE setPropertyA NOTIFY propertyCChanged)
    Q_PROPERTY(int propertyB READ propertyB WRITE setPropertyB NOTIFY propertyCChanged)
    Q_OBJECT
public:
   ClassB bObject;  
   Q_INVOKABLE int propertyB() { 

      return bObject.property("propertyB"); 
   }
}
mike510a
  • 2,102
  • 1
  • 11
  • 27