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. Enum/Flags from C++ to QML
Forum Updated to NodeBB v4.3 + New Features

Enum/Flags from C++ to QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
8 Posts 4 Posters 1.6k 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.
  • M Offline
    M Offline
    Mark81
    wrote on last edited by
    #1

    From here and here I'm trying to expose to QML this enum (or flag following the Qt meaning):

    settings.h

    #ifndef SETTINGS_H
    #define SETTINGS_H
    
    #include <QObject>
    
    class Settings : public QObject
    {
        Q_OBJECT
    
    public:
        enum GestureType
        {
            Unknown,
            Left,
            Right,
            Up,
            Down,
            Far,
            Near
        };
        Q_DECLARE_FLAGS(GestureTypes, GestureType)
        Q_FLAG(GestureTypes)
    
        explicit Settings() { }
    };
    
    #endif // SETTINGS_H
    

    main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    
    #include "settings.h"
    
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
        QQmlApplicationEngine engine;
        Settings settings;
    
        const QUrl url(QStringLiteral("qrc:/main.qml"));
        QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl)
        {
            if (!obj && url == objUrl) QCoreApplication::exit(-1);
        }, Qt::QueuedConnection);
    
        engine.rootContext()->setContextProperty("settings", &settings);
        engine.load(url);
    
        return app.exec();
    }
    

    main.qml

    console.log(settings.Near);
    

    is undefined, the same if I try to use settings.GestureTypes.Near.

    Why?

    J.HilkJ 1 Reply Last reply
    0
    • GrecKoG Online
      GrecKoG Online
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      You have to register the Setting type to QML

      M 1 Reply Last reply
      0
      • M Mark81

        From here and here I'm trying to expose to QML this enum (or flag following the Qt meaning):

        settings.h

        #ifndef SETTINGS_H
        #define SETTINGS_H
        
        #include <QObject>
        
        class Settings : public QObject
        {
            Q_OBJECT
        
        public:
            enum GestureType
            {
                Unknown,
                Left,
                Right,
                Up,
                Down,
                Far,
                Near
            };
            Q_DECLARE_FLAGS(GestureTypes, GestureType)
            Q_FLAG(GestureTypes)
        
            explicit Settings() { }
        };
        
        #endif // SETTINGS_H
        

        main.cpp

        #include <QGuiApplication>
        #include <QQmlApplicationEngine>
        #include <QQmlContext>
        
        #include "settings.h"
        
        int main(int argc, char *argv[])
        {
            QGuiApplication app(argc, argv);
            QQmlApplicationEngine engine;
            Settings settings;
        
            const QUrl url(QStringLiteral("qrc:/main.qml"));
            QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl)
            {
                if (!obj && url == objUrl) QCoreApplication::exit(-1);
            }, Qt::QueuedConnection);
        
            engine.rootContext()->setContextProperty("settings", &settings);
            engine.load(url);
        
            return app.exec();
        }
        

        main.qml

        console.log(settings.Near);
        

        is undefined, the same if I try to use settings.GestureTypes.Near.

        Why?

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @Mark81

        you need to register your enum:

        qmlRegisterUncreatableType<Settings> ("SettingsEnumImport", 1, 0, "NameSpace", "Some Message");

        where you want to use it in qml:

        import SettingsEnumImport 1.0
        
        ...
        ...
        
        console.log(NameSpace.Near)
        

        not sure if it will work with the Q_FLAG macro, but it will with Q_ENUM(GestureTypes)


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        M 1 Reply Last reply
        1
        • GrecKoG GrecKo

          You have to register the Setting type to QML

          M Offline
          M Offline
          Mark81
          wrote on last edited by
          #4

          @GrecKo Not did I?

          engine.rootContext()->setContextProperty("settings", &settings);
          

          What then this line does?

          KroMignonK 1 Reply Last reply
          0
          • J.HilkJ J.Hilk

            @Mark81

            you need to register your enum:

            qmlRegisterUncreatableType<Settings> ("SettingsEnumImport", 1, 0, "NameSpace", "Some Message");

            where you want to use it in qml:

            import SettingsEnumImport 1.0
            
            ...
            ...
            
            console.log(NameSpace.Near)
            

            not sure if it will work with the Q_FLAG macro, but it will with Q_ENUM(GestureTypes)

            M Offline
            M Offline
            Mark81
            wrote on last edited by Mark81
            #5

            @J-Hilk it seems to work, thanks! But in the links I provided there is no mention of qmlRegisterUncreatableType nor about the needs of import the domain. How should I guess from the docs? It's a real question, because if I understand how to read the docs I would solve most of the problems just reading the manual...

            J.HilkJ KroMignonK 2 Replies Last reply
            0
            • M Mark81

              @GrecKo Not did I?

              engine.rootContext()->setContextProperty("settings", &settings);
              

              What then this line does?

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by KroMignon
              #6

              @Mark81 said in Enum/Flags from C++ to QML:

              engine.rootContext()->setContextProperty("settings", &settings);

              What then this line does?

              As described in documentation, this will define a property called "settings" in the root context.
              This property will be linked to local variable settings.

              qmlRegisterUncreatableType<Settings> ("SettingsEnumImport", 1, 0, "NameSpace", "Some Message"); will register a new type called NameSpace in namespace SettingsEnumImport 1.0 . This type in uncreatable, which means you cannot create a property/variable with type.

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              1 Reply Last reply
              1
              • M Mark81

                @J-Hilk it seems to work, thanks! But in the links I provided there is no mention of qmlRegisterUncreatableType nor about the needs of import the domain. How should I guess from the docs? It's a real question, because if I understand how to read the docs I would solve most of the problems just reading the manual...

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                @Mark81 said in Enum/Flags from C++ to QML:

                I provided there is no mention of qmlRegisterUncreatableType nor about the needs of import the domain

                mmh 🤔, I remember that differently. I actually found this solution myself in one of the official Qt examples.

                But even that I'm unable to find right now. Maybe the method changed, or was supposed to change / now work without a call to qmlRegisterUncreatableType


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                0
                • M Mark81

                  @J-Hilk it seems to work, thanks! But in the links I provided there is no mention of qmlRegisterUncreatableType nor about the needs of import the domain. How should I guess from the docs? It's a real question, because if I understand how to read the docs I would solve most of the problems just reading the manual...

                  KroMignonK Offline
                  KroMignonK Offline
                  KroMignon
                  wrote on last edited by
                  #8

                  @Mark81 said in Enum/Flags from C++ to QML:

                  But in the links I provided there is no mention of qmlRegisterUncreatableType nor about the needs of import the domain

                  Then you don't have read carefully the documentation.
                  To get access to enum on QML side, you have to register it (cf. Enumeration Type documentation)

                  Providing the Message class has been registered with the QML type system.

                  The given link is Registering C++ Types with the QML Type System

                  There are multiple way to achieve this, depending on Qt Version you are using and build system (qmake or cmake).
                  But the way exposed by @J-Hilk works in every case.

                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                  1 Reply Last reply
                  2

                  • Login

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