Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QObject with property-like members: use owning class for signals/slots



  • Hello!

    Use of signal/slot system requires type to descend from QObject and have Q_OBJECT macro as part of its declaration in order for QtMOC to process it. And the recommended way to implement object's properties is to implement accessor methods and signals by hand.

    Although, this causes a lot of boilerplate in my case where I have multiple properties with almost identical logic, and kind of pollution of class scope. So I wondered if I can define "property" class which is always a member of certain QObject-derived class and has its own set of methods and signals. And consequently have not

    someobject.setProperty(...);
    // ...
    emit someobject.propertyChanged(...);
    

    but

    someobject.property.set(...);
    // ...
    emit someobject.property.changed(...);
    

    This is definitely doable if I derive my property class from QObject. Although this looks a bit redundant since property instance does not exist apart from its containing object.

    So my question is. Is there some way to tell Qt to implement signal/slot bindings for certain type through its mandatory owner type?

    Thanks


  • Lifetime Qt Champion

    Hi,

    If you have properties that a repeating but changing only in implementation, why not have a base class which defines them and then use subclasses where you implement the methods that are different ?


Log in to reply