1

I have this class, register it to use as 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

myTask->setData(2,Qt::UserRole,
QVariant::fromValue(MyTimeLine(a,b)));

MyTimeLine taskTimeLine = qvariant_cast<MyTimeLine>(index.data());
//taskTimeLine._kStartTime = 0;
//taskTimeLine._kEndTime = 1; 
user3510417
  • 13
  • 1
  • 3

1 Answers1

2

Are your sure the index.data() can be converted to 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 () const

Returns the stored value converted to the template type T. Call canConvert() to find out whether a type can be converted. If the value cannot be converted, default-constructed value will be returned.

Kirell
  • 9,228
  • 4
  • 46
  • 61