Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing pointers using QListWidgetItem::setData

Tags:

qt

qlistwidget

I have a QListWidget of calendars. Each QListWidgetItem is logically associated with an instance of Calendar, which is a class that belongs to the Model side of the application.

Can I store this association in the form of a pointer using QListWidgetItem::setData? When I attempt to do this, I get the following error:

error: 'QVariant::QVariant(void*)' is private

like image 769
Pieter Avatar asked Feb 12 '12 10:02

Pieter


1 Answers

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.

like image 107
alexisdm Avatar answered Oct 16 '22 19:10

alexisdm