开发者

Changing contents of ItemData

I store objects of a custom data type in QSta开发者_运维技巧ndardListItems. I recover these objects by calling:

i.data(Qt::UserRole + 1).value<LiteReach>();

This only creates a new object in the stack. Any changes I do to them would be temporary.

Is there a way to get the base object stored in itemData so that it could be manipulated directly? If not what would be the preferred method to change itemData?

I would like not to call setData each time an object is modified since it would consume a lot of resources.


You could use pointers that allow access to the concrete data objects instead of copying the whole data into a QVariant like above.

The problem is that value() returns a copy of your data. So if you make any modifications, they will be gone as soon as the copy is removed from stack.

If you don't want to use pointers, I guess you'll have to stick with setData().


I was under the impression that the QVariant class was not intended for pointer storage.

However, if you really want to do it, you could always do something like

LiteNode* myPtr = new LiteNode;
QVariant v(reinterpret_cast<quint32>(myPtr)); //or quint64 for 64-bit apps
myPtr = reinterpret_cast<LiteNode*>(v.value<quint32>());

That way you don't even have to declare your object type as a Qt Metatype.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜