How can I cast a QVariant to custom class?
You could try using qvariant_cast and qobject_cast.
QObject *object = qvariant_cast<QObject*>(selectItem);
Category *category = qobject_cast<Category*>(object);
Also, never put any persistent statement into Q_ASSERT. It will not be used when the assert is not enabled.
EDIT: works for non QObject derived type (see Final Contest's answer for this case)
First of all, you need to register your type to be part of QVariant managed types
//customtype.h
class CustomType {
};
Q_DECLARE_METATYPE(CustomType)
Then you can retrieve your custom type from QVariant
in this way :
CustomType ct = myVariant.value<CustomType>();
which is equivalent to:
CustomType ct = qvariant_cast<CustomType>(myVariant);