Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QMetaProperty::read: Unable to handle unregistered datatype 'TreeItem<InspectorItem>*'

Tags:

c++

qt

qml

Qt doesn't allow to register class template?

My class hierarchy is

TreeItemTemplateBackend : public QObject

template<typename T>
TreeItem : public TreeItemTemplateBackend

This is what i registred in qml:

qmlRegisterType<InspectorItem>("ge.gui", 1, 0, "InspectorItem");
qmlRegisterType<TreeItemTemplateBackend>("ge.gui", 1, 0, "TreeItemTemplateBackend");
qmlRegisterType<TreeItem<InspectorItem>>("ge.gui", 1, 0, "TreeItem");

I am still getting this error:

QMetaProperty::read: Unable to handle unregistered datatype TreeItem<InspectorItem>* for property 'Inspector::root'

Inspector::root is:

Q_PROPERTY(TreeItem<InspectorItem> * root READ root NOTIFY rootChanged)
like image 401
Krab Avatar asked Jun 15 '14 16:06

Krab


2 Answers

when you want to use a pointer to e.g. 'ClassA' in a Q_PROPERTY, you have to register it like this:

qRegisterMetaType<ClassA*>("ClassA*");

The solution might be more complex for you due to the use of templates, but hopefully this points you in the right direction.

(same as my other answer. source: 'jpn')

like image 124
Aralox Avatar answered Oct 23 '22 10:10

Aralox


You have to do two things:

  1. Register your type with macro Q_DECLARE_METATYPE( ClassName* ) for each template realisation. Pay an attention on asterisk (*) in the end! Since you need to expose pointer, not the value. For example Q_DECLARE_METATYPE( ClassName<ItemClass>* ). Look into documentation where to put this declaration the best (after class declaration out of all namespaces usually ). There are also issues with namespaces, so always use full qualified names here.
  2. Register each template realisation with qmlRegisterType<ClassName>(...) or qmlRegisterUncreatableType<ClassName>(...) if you need just expose some data with a property and not to instantiate it in QML. Here you don't need an asterisk, since you register not the pointer, but class itself.
like image 41
koldoon Avatar answered Oct 23 '22 10:10

koldoon