Storing pointers using QListWidgetItem::setData

对着背影说爱祢 提交于 2019-11-30 12:38:40

There is another constructor for void*: QVariant::QVariant(int typeOrUserType, const void * copy) where you should pass an unique integer to represent the pointer type.

But as stated by the documentation, you could declare your pointer type with Q_DECLARE_METATYPE(Calendar*) and use QVariant::fromValue<Calendar*>(...) and QVariant::value<Calendar*>() to store and retrieve the value.

Or instead, because you are using a QListWidget instead of a regular model, you can just subclass QListWidgetItem, and add a Calendar* member variable with the required accessors, to avoid the overhead of using QVariant.

I would suggest looking at this solution as well, which I think is quite elegant: (there are minor syntax errors, but you will spot them quickly or the compiler will issue an error)

http://blog.bigpixel.ro/2010/04/storing-pointer-in-qvariant/

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!