[英]QVariant custom classes polymorphism
I have such a classes hierarchy: 我有这样的类层次结构:
class Base {
...
virtual QWidget* getEditor();
...
}
class Derived {
...
QWidget* getEditor() Q_DECL_OVERRIDE;
...
}
Both classes are registered via Q_DECLARE_METATYPE() 这两个类都通过Q_DECLARE_METATYPE()注册
I'm getting instance of Base class from QVariant. 我从QVariant获取基类的实例。 Is it possible to get a pointer from QVariant to be able to invoke
getEditor()
from a Derived object? 是否可以从QVariant获取指针 ,以便能够从派生对象调用
getEditor()
?
I'm trying this atm but have no success: 我正在尝试此atm,但没有成功:
if (index.data(Qt::EditRole).canConvert<Base>())
return index.data(Qt::EditRole).value<Base>().getEditor(parent);
This snipper invokes Base class method. 此侦听器调用基类方法。
You need to make your base class' function virtual
to enable polymorphism: 您需要将基类的函数
virtual
以启用多态性:
class Base {
...
virtual QWidget* getEditor();
...
}
class Derived {
...
QWidget* getEditor() Q_DECL_OVERRIDE;
...
}
Also, the way you have it now will cause object slicing . 而且,您现在拥有的方式将导致对象切片 。 You will need to acquire a pointer to
Derived
, and call the function on that pointer. 您将需要获取一个指向
Derived
的指针,并在该指针上调用函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.