Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, January 2008
QVariant Question


Message 1 in thread

I'm running into a little problem with QVariant in a QListWidgetItem.

Currently I'm setting my value like this:

item->setData(Qt::UserRole, (int)library);

And retrieving it later like this:

Library* library = (Library*)current->data(Qt::UserRole).value<int>();

This basically works...but it's not what I expected I'd have to do. My
initial approach looked like this:

Set Data:
item->setData(Qt::UserRole, library);

Get Data:

Library* library = current->data(Qt::UserRole).value<Library*>();

I'd much prefer it that way rather than casting the pointer to/from int.

However, here is where the problem comes in when I do it that way:

/usr/local/Trolltech/Qt-4.3.3/include/QtCore/qmetatype.h: In static
member function âstatic int QMetaTypeId2<T>::qt_metatype_id() [with T =
Library*]â:
/usr/local/Trolltech/Qt-4.3.3/include/QtCore/qmetatype.h:176:
instantiated from âint qMetaTypeId(T*) [with T = Library*]â
/usr/local/Trolltech/Qt-4.3.3/include/QtCore/qvariant.h:541:
instantiated from âT qvariant_cast(const QVariant&) [with T = Library*]â
/usr/local/Trolltech/Qt-4.3.3/include/QtCore/qvariant.h:554:
instantiated from âT qVariantValue(const QVariant&) [with T = Library*]â
/usr/local/Trolltech/Qt-4.3.3/include/QtCore/qvariant.h:293:
instantiated from âT QVariant::value() const [with T = Library*]â
ui/PlacePartDialog.cpp:115:   instantiated from here
/usr/local/Trolltech/Qt-4.3.3/include/QtCore/qmetatype.h:166: error:
âqt_metatype_idâ is not a member of âQMetaTypeId<Library*>â

So for some odd reason, it does not like my own class being passed in as
a Type even though it's just a pointer to that class.

Can anyone explain to me why this doesn't work and what I'm doing on?

QT classes such as QColor, etc. work perfectly fine but I don't see any
distinct differences between that class and my own class.

Thanks,

Stephan




--
 [ signature omitted ] 

Message 2 in thread

See http://doc.trolltech.com/4.3/qmetatype.html#Q_DECLARE_METATYPE

-- 
 [ signature omitted ] 

Message 3 in thread

On Mon, 2008-01-07 at 20:44 +0200, J-P Nurmi wrote:
> See http://doc.trolltech.com/4.3/qmetatype.html#Q_DECLARE_METATYPE
> 

Thanks, that did it. =)

Might be useful for QT though to put a reference to that in the QVariant
documentation somewhere.

Thanks again,

Stephan


--
 [ signature omitted ]