[英]qvariant_cast with custom type
I have this class, register it to use as QVariant 我有这个课程,注册它以用作QVariant
class MyTimeLine
{
public:
MyTimeLine(double kStart = 0, double kEnd = 1);
void paint(QPainter *painter, const QRect &rect,
const QPalette &palette)const;
private:
double _kStartTime;
double _kEndTime;
};
Q_DECLARE_METATYPE(MyTimeLine)
Then i convert it in QVariant, and when i want use qvariant_cast and get MyTimeLine object i get object, created by default constructor 然后我在QVariant中将其转换,当我想使用qvariant_cast并获取MyTimeLine对象时,我获取了由默认构造函数创建的对象
myTask->setData(2,Qt::UserRole,
QVariant::fromValue(MyTimeLine(a,b)));
MyTimeLine taskTimeLine = qvariant_cast<MyTimeLine>(index.data());
//taskTimeLine._kStartTime = 0;
//taskTimeLine._kEndTime = 1;
Are your sure the index.data() can be converted to MyTimeLine ? 您确定index.data()可以转换为MyTimeLine吗?
T qvariant_cast ( const QVariant & value )
Returns the given value converted to the template type T. This function is equivalent to QVariant::value(). 返回转换为模板类型T的给定值。此函数等效于QVariant :: value()。
T QVariant::value () const
Returns the stored value converted to the template type T. Call canConvert() to find out whether a type can be converted. 返回转换为模板类型T的存储值。调用canConvert()来确定是否可以转换类型。 If the value cannot be converted, default-constructed value will be returned. 如果无法转换该值,则将返回默认构造的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.