Results 1 to 8 of 8

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

  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 13:07.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    Shouldn't that be

    Qt Code:
    1. Enums.MONDAY
    To copy to clipboard, switch view to plain text mode 
    in the QML code?

    Cheers,
    _

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

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

    Thank you for answering!
    No, the result is
    qrc:/main.qml:17: Error: Unknown method parameter type: Enums::DayOfWeek
    Actually, the separation of all the Enums into different classes (e.g. having to specify them with "DayOfWeek") seems quite useful for me. From anywhere in the cpp code I access the enums like this:
    Qt Code:
    1. Enums::DayOfWeek::MONDAY
    To copy to clipboard, switch view to plain text mode 

    I can't register the single Enums explicitly (qmlRegisterUncreatableType<Enums::DayOfWeek>()), because enum class is not a QObject. The problem has also been brought up - but not solved - here.
    Last edited by sedi; 21st March 2016 at 13:44.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    Quote Originally Posted by sedi View Post
    No, the result is
    Hmm, ok, probably need an int there.

    Quote Originally Posted by sedi View Post
    Actually, the separation of all the Enums into different classes (e.g. having to specify them with "DayOfWeek") seems quite useful for me. From anywhere in the cpp code I access the enums like this:
    Qt Code:
    1. Enums::DayOfWeek::MONDAY
    To copy to clipboard, switch view to plain text mode 
    Looks like C++ syntax to me, never seen :: as a qualifier separator in JavaScript.

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    sedi (21st March 2016)

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

    Thumbs up Re: "Enum class"/Q_ENUM use in QML? Cannot read property of undefined

    As for the :: syntax: yes, this is what I do from C++
    "From anywhere in the cpp code I acc.."
    I'd thought there should be a syntax like:
    Enums.DayOfWeek.MONDAY
    in QML.

    It seems a bit unsatisfying to either use deprecated stuff or not use the enums in QML at all. I will probably file a bug about this.

    For now I will probably work with int interfaces. Thank you for your time and effort anda_skoa! Cheers!!

  7. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    Quote Originally Posted by sedi View Post
    As for the :: syntax: yes, this is what I do from C++
    I'd thought there should be a syntax like: in QML.
    JavaScript doesn't have enums, QML allows you to do something like enums by registering names.
    What you are writing there is an Object or singleton called Enums with a subobject/property DayOfWeek with an enum.
    But that doesn't exist on the C++ side either.

    Quote Originally Posted by sedi View Post
    It seems a bit unsatisfying to either use deprecated stuff or not use the enums in QML at all
    Or you use normal enums instead of enum classes and have comparable support on both language sides.
    Would be my choice.

    Cheers,
    _

  8. #7
    Join Date
    Jul 2016
    Posts
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

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

    I've had a similar issue. Does it work if you add

    Qt Code:
    1. qRegisterMetaType<Enums::DayOfWeek >("Enums::DayOfWeek")
    To copy to clipboard, switch view to plain text mode 

    and change the call to:

    Qt Code:
    1. myObject.outputDay(Enums.MONDAY)
    To copy to clipboard, switch view to plain text mode 
    ?

    The one thing that is not so nice about this is that the parameter in the call doesn't refer to DayOfWeek anymore.

    Regards

  9. The following user says thank you to oiaavb for this useful post:

    sedi (1st August 2016)

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

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

    Yes, absolutely! The loss of the strong typisation is a pity - but at least I can use it :-) Thanks!

Similar Threads

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