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++ enum in QML is undefined

C++ enum in QML is undefined

Scheduled Pinned Locked Moved QML and Qt Quick
c++qmlproblem
3 Posts 2 Posters 9.5k Views
  • 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
    Spez
    wrote on last edited by
    #1

    Hi there,

    I got a problem using C++ enums in my QML code

    Here is the class definition containing the enumeration I want to expose in App.hpp

    #ifndef APP_HPP
    #define APP_HPP
    
    #include <QObject>
    
    class App : public QObject {
        Q_OBJECT
        Q_ENUMS(View)
    public:
        enum View {
            MainView,
            BrowserView,
            SettingsView
        };
    };
    
    #endif
    

    here is the main.cpp file

    #include <QApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    #include "App.hpp"
    
    int main(int argc, char *argv[]) {
        QApplication application(argc, argv);
        App app;
        QQmlApplicationEngine engine;
        engine.rootContext()->setContextProperty("App", &app);
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        return application.exec();
    }
    

    and here is the actual QML code in main.qml

    import QtQuick 2.4
    import QtQuick.Window 2.2
    
    ApplicationWindow {
        title: qsTr("My App")
        visible: true
        width: 640
        height: 480
        Text {
            text: "main view: " + App.MainView
        }
    }
    

    The result is not satisfying, this is what I get, an undefined value

    After a long while trying to find a solution by myself I gave up, I still don't understand what I could have done wrong.
    Could somebody please help me out with that?

    1 Reply Last reply
    1
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Here you can find a nice explanation on how to do it

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Here you can find a nice explanation on how to do it

        S Offline
        S Offline
        Spez
        wrote on last edited by
        #3

        @SGaist
        I finally figured it out now, the problem was that I didn't register my class in main.cpp using the qmlRegisterType from <QtQml>. The code below works

        #include <QApplication>
        #include <QQmlApplicationEngine>
        #include <QQmlContext>
        #include <QtQml>
        #include "App.hpp"
        
        int main(int argc, char *argv[]) {
            QApplication application(argc, argv);
            App app;
            QQmlApplicationEngine engine;
            qmlRegisterType<App>("SomeLib", 1, 0, "App");
            engine.rootContext()->setContextProperty("App", &app);
            engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
            return application.exec();
        }
        

        main.qml

        import QtQuick 2.4
        import QtQuick.Window 2.2
        import QtQuick.Controls 1.3
        import SomeLib 1.0
        
        ApplicationWindow {
            title: qsTr("My App")
            visible: true
            width: 640
            height: 480
            Text {
                text: "main view: " + App.BrowserView
            }
        }
        
        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