C++ Q_ENUM autocompletion in QtCreator
-
Hello everyone!
I have implemented class that helps to use c++ enums in QML (base approach):
class SomeClass { Q_GADGET public: enum Value { UNDEFINED = -1, KNOWN }; Q_ENUM(Value); static void registerQML() { qmlRegisterUncreatableType<SomeClass >("App.Enums", 1, 0, "SomeClass", "SomeClass"); qRegisterMetaType<SomeClass ::Value>("SomeClass ::Value"); } }
It works and I can use enums with autocompletion in QML.
But then I want to make more flexible approach and create the following base class to register all derived enums:
template<typename T> static void registerQML() { QString typeName = QString(typeid (T).name()).split(" ").last(); qmlRegisterUncreatableType<T>("PlastApp.Enums", 1, 0, qPrintable(typeName), qPrintable(typeName)); qRegisterMetaType<typename T::Value>(qPrintable(QString("%1::Value").arg(typeName))); }
autocompletion stops working, but QML gets correct values and works as usual. What can be the reason and how to fix it?
-
I am assuming this is called before you create your qml engine.
I wonder if being a template makes it hard for the IDE to determine if a type is registered. I assume Qt does a parse to determine what objects/values are available to the qml code. I have seen this with context properties. If the property is simple string defined in the function call it seems to find the value for autocomplete. But if the context property is a derived string made from parts and assembled then it does not seem to find it with autocomplete.
-
@fcarney Thank you for the answer!
I have registered it in the same place before qml engine.
Yes, I've got the same idea about parsing by strings when had faced with this behaviour. I have tested the macros approach (as it was shown in https://github.com/carlonluca/lqtutils), but it still has no autocompletion. Well, looks like there is no way to do it in a more comfortable way, because it's performing by IDE before even preprocessor stage.