How to share property in different classes?



  • In my project there are many classes some of which have the same property, I want to reuse those property definitions in different classes, but could not find a way.
    E.g: There are three properties:name, pic, desc which are used by class A,B,C. I don't want to write the properties multiple times in different class. Is there anyway to do that?
    @class A: public QObject
    {
    Q_OBJECT
    Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
    Q_PROPERTY(QString pic READ pic WRITE setPic NOTIFY picChanged)
    public:
    A(QObject *parent = 0);

    QString name();
    void setName(QString name);
    
    QString pic();
    void setPic(QString pic);
    

    signals:
    void nameChanged();
    void picChanged();

    private:
    QString m_name;
    QString m_pic;
    }

    class B: public QObject
    {
    Q_OBJECT
    Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
    Q_PROPERTY(QString desc READ desc WRITE setDesc NOTIFY descChanged)
    public:
    B(QObject *parent = 0);

    QString name();
    void setName(QString name);
    
    QString desc();
    void setDesc(QString desc);
    

    signals:
    void nameChanged();
    void descChanged();

    private:
    QString m_name;
    QString m_desc;
    }

    class C: public QObject
    {
    Q_OBJECT
    Q_PROPERTY(QString pic READ pic WRITE setPic NOTIFY picChanged)
    Q_PROPERTY(QString desc READ desc WRITE setDesc NOTIFY descChanged)
    public:
    C(QObject *parent = 0);

    QString pic();
    void setPic(QString pic);
    
    QString desc();
    void setDesc(QString desc);
    

    signals:
    void picChanged();
    void descChanged();

    private:
    QString m_pic;
    QString m_desc;
    }@



  • -Sounds like classic example for inheritance.-

    I've just realised that your classes use different properties.

    You might create a template class which has storage, a setter and a getter for a property (and see if such a case is supported by the Q_PROPERTY implementation).
    Or you use a preprocessor macro (or a combination of both) which expands to the actual code.



  • [quote author="Lukas Geyer" date="1312642365"]Sounds like classic example for inheritance.[/quote]

    No, can not use multiple inheritance, because multiple QObject inheritance is not supported, but Q_PROPERTY definition needs class to inherit from QObject.



  • [quote author="Alex Liu" date="1312642738"]
    [quote author="Lukas Geyer" date="1312642365"]Sounds like classic example for inheritance.[/quote]

    No, can not use multiple inheritance, because multiple QObject inheritance is not supported, but Q_PROPERTY definition needs class to inherit from QObject.

    [/quote]

    There is no need to inherit QObject multiple times as the base class already inherits QObject (which allows for using properties).



  • @Lukas Geyer
    Could you give some example code which B,C can share the same code for property desc, A,B for name, A,C for pic? I tried use MICRO, it would not work.



  • I just tried the template solution and passing member functions as setters and getters to Q_PROPERTY does, as suspected, not work.

    The preprocessor solution does work as expected.
    @
    #define PROPERTY(Type, Name) private:
    Type Name;
    public:
    void set##Name(Type value) { Name = value; }
    Type get##Name() const { return Name; }

    // ...

    class A : public QObject
    {
    Q_OBJECT

    Q_PROPERTY(QString name READ getname WRITE setname)
    Q_PROPERTY(QString description READ getdescription WRITE setdescription)
    
    PROPERTY(QString, name)
    PROPERTY(QString, description)
    

    };
    @
    You cannot include the Q_PROPERTY macro in the PROPERTY macro as the source files are moc'ed before preprocessed and moc does not replace in macros.



  • Thanks Lukas, I even don't want to write Q_PROPERTY definition. But as you mentioned, because of the moc, MACRO for define Q_PROPERTY does not work, and in other hand, multiple inheritance is not work too. So I think I need to write properties for each class separately.



  • Hi,

    MOC can't work with #defines, we also wanted to do such things :-(
    BUt it just scans the source code and has no pre processor working in front.

    So you will have to type it in each class seperately...



  • Thanks guys, looks I still need to manually write the code many times:(



  • Though it has been quite old post, I think it's interesting topic. Hmm. How about introducing another simple moc-like code generating tool for this, if there is a certain reason why moc cannot not support getter/setter generation like this.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.