Hi,

The strangest thing...

I'm filling QListWidgetItems with a template type T* p_Object (i've added the Q_DECLARE_METATYPE() macro to the candidate classes for T). Amazingly, it works fine, i was pleasantly surprised: after storing T* as a Qt::UserRole, i immediately read back the data to check, and there are no problems.

But... reading the data back in a later pass (using the same ItemToObject() function), it fails. The QVariant seems ok (has the same id number) but the qVariantValue<T*> "cast" fails. I'm not changing anything in the QListWidgetItems inbetween passes...

Any idea's?

Kind Regards,
Daniel Dekkers

Qt Code:
  1. template <class T>
  2. void
  3. c_ListViewController<T>::SetItemData(QListWidgetItem* p_Item, T* p_Object)
  4. {
  5. p_Item->setData(Qt::DisplayRole, QString::fromStdWString(p_Object->Name()));
  6.  
  7. QVariant l_Data;
  8. l_Data.setValue(p_Object);
  9. p_Item->setData(Qt::UserRole, l_Data);
  10.  
  11. // Check...
  12. T* l_ReadBackObject = GetItemData(p_Item);
  13. if (p_Object!=l_ReadBackObject)
  14. {
  15. c_MessageDialog* l_MessageDialog = new c_MessageDialog(this, "Something wrong..."); l_MessageDialog->exec();
  16. }
  17. }
  18.  
  19. // =============================================================================
  20.  
  21. template <class T>
  22. T*
  23. c_ListViewController<T>::GetItemData(QListWidgetItem* p_Item) const
  24. {
  25. T* l_ReadBackObject = NULL;
  26. QVariant l_ReadBackData = p_Item->data(Qt::UserRole);
  27. l_ReadBackObject = qVariantValue<T*>(l_ReadBackData);
  28. return l_ReadBackObject;
  29. }
To copy to clipboard, switch view to plain text mode