如何覆盖Q_Property? [英] How to override a Q_Property?

查看:109
本文介绍了如何覆盖Q_Property?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下课程:

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

    ...
};

访问属性值时,将直接调用A类方法,而不是B类方法.

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)?

推荐答案

来自Qt文档:

可以继承READ,WRITE和RESET函数.他们也可以 是虚拟的.当它们在多个 使用继承,它们必须来自第一个继承的类.

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.

只需将访问器虚拟化,它们就会从vtable中调用,因此您将为每种不同的子类型获得正确的功能.

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

这篇关于如何覆盖Q_Property?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆