QTreeWidgetItem error while get data
Solved
General and Desktop
-
Hi,
I would like my QTreeWidgetItem class objects to hold one of the Animal enum values defined outside the class
enum Animal{ Cat, Dog, Snake};
QTreeWidgetItem *phaseItem = new QTreeWidgetItem(); phaseItem->setText(0, "dog); phaseItem->setFlags(phaseItem->flags() | Qt::ItemIsUserCheckable | Qt::ItemIsSelectable); phaseItem->setCheckState(0, Qt::Unchecked); phaseItem->setData(0, Qt::UserRole, Animal::Dog); addTopLevelItem(phaseItem);
I connected the signal to the slot and while unchecking the checkbox I would like to get the enum value
QObject::connect(this, &QTreeWidget::itemChanged, this, &SeriesManager::treeWidgetItemChanged);
void SeriesManager::treeWidgetItemChanged(QTreeWidgetItem *item, int i) { QVariant v = item->data(0, Qt::UserRole); Phase phase = v.value<Animal>(); }
But get error:
QtCore\qglobal.h:688: error: static assertion failed: Type is not registered, please use the Q_DECLARE_METATYPE macro to make it known to Qt's meta-object system QtCore\qmetatype.h:1476: error: 'qt_metatype_id' is not a member of 'QMetaTypeId<Animal>' QtCore\qmetatype.h:1476: error: body of constexpr function 'static constexpr int QMetaTypeId2<T>::qt_metatype_id() [with T = Animal]' not a return-statement
How could I reseived my enum data from function "data()"?
-
Hi,
@Creatorczyk said in QTreeWidgetItem error while get data:
QtCore\qglobal.h:688: error: static assertion failed: Type is not registered, please use the Q_DECLARE_METATYPE macro to make it known to Qt's meta-object system
You have here what to do to be able to do what you want.