Results 1 to 2 of 2

Thread: Storing and reading back template types in QVariant

  1. #1
    Join Date
    Feb 2010
    Qt products
    MacOS X Windows

    Default Storing and reading back template types in QVariant


    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()));
    7. QVariant l_Data;
    8. l_Data.setValue(p_Object);
    9. p_Item->setData(Qt::UserRole, l_Data);
    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. }
    19. // =============================================================================
    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 

  2. #2
    Join Date
    Mar 2008
    Thanked 3 Times in 2 Posts
    Qt products
    Unix/X11 Windows Symbian S60

    Default Re: Storing and reading back template types in QVariant

    I use for storing a value qVariantFromValue(myValue) and to get it back variant.value<MyCustomValue>(). Have your tried them?

Similar Threads

  1. Meta Types and QVariant
    By kroenecker in forum Qt Programming
    Replies: 4
    Last Post: 26th February 2007, 08:48
  2. QVariant::toString and custom data types
    By Vladimir in forum Qt Programming
    Replies: 2
    Last Post: 16th January 2007, 16:36
  3. QVariant - storing QIcons[]
    By moowy in forum Qt Programming
    Replies: 7
    Last Post: 25th August 2006, 01:41
  4. Use QVariant with custon data types
    By mcosta in forum Qt Programming
    Replies: 4
    Last Post: 11th January 2006, 15:55
  5. QVariant types support for custom properties
    By Dusdan in forum Qt Tools
    Replies: 9
    Last Post: 11th January 2006, 10:55

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.