11

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) 
3
  • Try registering the pointer type instead of the class type? Commented Jun 15, 2014 at 18:51
  • 1
    @Chris: got another errors: type 'TreeItem<InspectorItem> *' cannot be used prior to '::' because it has no members Commented Jun 15, 2014 at 19:11
  • @Chris: i still don't know if it is even possible to register template class Commented Jun 15, 2014 at 19:11

2 Answers 2

20

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')

Sign up to request clarification or add additional context in comments.

1 Comment

When I do this I get an exception in QScopedPointer.h
2

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.

1 Comment

Actually, step 1 is not needed for QObject-derived classes, and step 2 is not needed for properties on objects, only to make full classes available for creating from QML or accessing enums.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.