C++ inheritance with Qml support

404 Views Asked by At

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

1

There are 1 best solutions below

0
mike510a On

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"); 
   }
}