Results 1 to 8 of 8

Thread: "Enum class"/Q_ENUM use in QML? Cannot read property of undefined

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2012
    Location
    Dortmund, Germany
    Posts
    159
    Thanks
    69
    Thanked 10 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Question "Enum class"/Q_ENUM use in QML? Cannot read property of undefined

    Hi, I use a number of enums in my project, which I keep in a single "Enums" class, each of them defined by the c+11 way of "enum class" and the new Q_ENUM macro (instead of Q_ENUMS, as proposed here).

    I don't get them to work in QML, though - and all I can find about this topic (there are actually plenty of solutions!) is using the older concepts.

    Below you find a minimized example project. On click, qml shall send Enums.DayOfWeek.MONDAY to a slot.
    The result is:
    qrc:/main.qml:17: TypeError: Cannot read property 'MONDAY' of undefined
    where I'd expext qDebug output from the slot (environment: Qt 5.5, QtCreator 3.6, Win7x64).

    Any ideas about how I could do it better / make it work?

    Several Enums in enums.h
    Qt Code:
    1. #ifndef ENUMS_H
    2. #define ENUMS_H
    3. #include <QObject>
    4.  
    5. class Enums
    6. {
    7. Q_GADGET
    8.  
    9. public:
    10. enum class DayOfWeek {
    11. SUNDAY=0,
    12. MONDAY=1,
    13. TUESDAY=2,
    14. WEDNESDAY=3,
    15. THURSDAY=4,
    16. FRIDAY=5,
    17. SATURDAY=6,
    18. UNDEFINED=-1
    19. };
    20. Q_ENUM(DayOfWeek)
    21.  
    22. enum class NameOrder {
    23. FIRST_FAMILY=0,
    24. FAMILY_FIRST=1
    25. };
    26. Q_ENUM(NameOrder)
    27.  
    28. };
    29.  
    30. #endif // ENUMS_H
    To copy to clipboard, switch view to plain text mode 

    An object with a slot that takes such an enum: someobject.h
    Qt Code:
    1. #ifndef SOMEOBJECT_H
    2. #define SOMEOBJECT_H
    3. #include <QObject>
    4. #include <QDebug>
    5. #include "enums.h"
    6.  
    7. class SomeObject : public QObject
    8. {
    9. Q_OBJECT
    10. public:
    11. explicit SomeObject(QObject *parent = 0): QObject(parent) {}
    12. public slots:
    13. void outputDay(Enums::DayOfWeek day) { qDebug()<<day; }
    14. };
    15.  
    16. #endif // SOMEOBJECT_H
    To copy to clipboard, switch view to plain text mode 

    This is being registered in main.cpp
    Qt Code:
    1. #include <QGuiApplication>
    2. #include <QQmlApplicationEngine>
    3. #include <QQmlEngine>
    4. #include <QtQml>
    5. #include "enums.h"
    6. #include "someobject.h"
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QGuiApplication app(argc, argv);
    11. QQmlApplicationEngine engine;
    12. qmlRegisterUncreatableType<Enums>("url.Enums", 1, 0, "Enums", "You cannot create an instance of the Enums.");
    13. qmlRegisterType<SomeObject>("url.SomeObject",1,0,"SomeObject");
    14. engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    15. return app.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 

    And I try to use it in main.qml
    Qt Code:
    1. import QtQuick 2.5
    2. import QtQuick.Window 2.2
    3. import url.SomeObject 1.0
    4. import url.Enums 1.0
    5.  
    6.  
    7. Window {
    8. visible: true
    9.  
    10. SomeObject {
    11. id: myObject
    12. }
    13.  
    14. MouseArea {
    15. anchors.fill: parent
    16. onClicked: {
    17. myObject.outputDay(Enums.DayOfWeek.MONDAY)
    18. }
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by sedi; 21st March 2016 at 12:07.

Similar Threads

  1. Replies: 1
    Last Post: 20th November 2015, 10:02
  2. Replies: 3
    Last Post: 16th March 2015, 07:31
  3. Cannot assign to non-existent property "lon" lon: "3"
    By TheIndependentAquarius in forum Qt Quick
    Replies: 1
    Last Post: 12th November 2013, 15:50
  4. [SOLVED] QPropertyAnimation "text" property in QLabel
    By AlekseyK in forum Qt Programming
    Replies: 3
    Last Post: 7th December 2010, 18:46
  5. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.