How to override a Q_Property?

别说谁变了你拦得住时间么 提交于 2019-12-24 07:46:28

问题


Consider these classes:

Class A : public QObject {
    ...
    Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged)

    virtual int value() { return m_value; }
    void setValue(int v) { m_value = v; Q_EMIT valueChanged();}
    ...

};

Class B : public A {
    ...

    int value() { return m_value * 2; }

    ...
};

When property value is accessed, the Class A method is called directly instead of Class B's.

So far, to workaround this apparent limitation I've replicated the property code and connected the signals from each class.

Is this the best solution?

Does anyone see potential problems (due to the properties having the same name)?


回答1:


From the Qt documentation:

The READ, WRITE, and RESET functions can be inherited. They can also be virtual. When they are inherited in classes where multiple inheritance is used, they must come from the first inherited class.

Just make the accessors virtual, and they will be invoked from the vtable, so you will get the right function for every different subtype.




回答2:


Q_PROPERTY ... // the same
signals:
    void valueChanged(int value);
public:
int value() const { return m_value; }
virtual void setValue(int newValue) { 
    m_value = newValue; 
    emit valueChanged( value() ); 
}


class B : public A {
public:
    void setValue(int newValue) { A::setValue(newValue *2); }
}

variant 2 (little bit better):

Class A {
Q_PROPERTY
signals:
    void valueChanged(int value);
public:
    int value(...
    void setValue(int value) {
        changeValue(value); // to call virtual changeValue
        emit valueChanged( m_value );
    }
protected:
    virtual void changeValue(int newValue) {
        m_value = newValue;
    }
...
}

// We dont touch public methods, but protected:
Class B : public A {
protected:
    /*virtual*/ void changeValue(int newValue) {
        A::changeValue(newValue *2);
    }
}


来源:https://stackoverflow.com/questions/29683341/how-to-override-a-q-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!