Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. C++ Q_ENUM autocompletion in QtCreator
Forum Updated to NodeBB v4.3 + New Features

C++ Q_ENUM autocompletion in QtCreator

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 300 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    St.Stanislav
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by
      #2

      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.

      C++ is a perfectly valid school of magic.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        St.Stanislav
        wrote on last edited by St.Stanislav
        #3

        @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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved