PDA

View Full Version : How to retrive data saved using Setdata?



parshant65@gmail.com
6th March 2014, 06:56
I have used QStandardItem::SetData to save data for each tree item, code is below:
m_htHashItem->setData(QVariant(QVariant::UserType, m_pCurrMapData), MyClassRole);

Now I need to retrieve it back on item click event
MAP_DATA* p = static_cast< MAP_DATA *> (index.data(MyClassRole).value());

but I am getting error:
error: C2783: 'T QVariant::value(void) const' : could not deduce template argument for 'T'

ChrisW67
6th March 2014, 07:14
Something like:

struct MAP_DATA { };
Q_DECLARE_METATYPE(MAP_DATA*)

// and assuming
MAP_DATA *m_pCurrMapData;

// Elsewhere
m_htHashItem->setData(QVariant(m_pCurrMapData), MyClassRole);

MAP_DATA* p = index.data(MyClassRole).value<MAP_DATA*>();

follows from the examples in the QVariant::value() docs

parshant65@gmail.com
6th March 2014, 08:31
Thanks for reply...not getting any error but getting NULL / 0 while getting value back.

struct MAP_DATA { };

enum MyRoles {
MyClassRole = Qt::UserRole + 1
};

Q_DECLARE_METATYPE(MAP_DATA*)

//set address of a linked list node
m_htHashItem->setData(QVariant(QVariant::UserType, m_pCurrMapData), MyClassRole);

//gettting same node
MAP_DATA* pFileData = index.data(MyClassRole).value<MAP_DATA*>();

But I am getting ZERO for pFileData everytime. Please let me know where am I wrong?

anda_skoa
6th March 2014, 10:07
Compare your setData() call with that ChrisW67 provided.
Notice the difference.
Fix.

Cheers,
_

parshant65@gmail.com
6th March 2014, 10:43
If I use SetData described by ChrisW67 then I am getting error in QT 5:
error: C2248: 'QVariant::QVariant' : cannot access private member declared in class 'QVariant'

anda_skoa
6th March 2014, 15:43
Try


m_htHashItem->setData(QVariant::fromValue(m_pCurrMapData), MyClassRole);


Cheers,
_