Results 1 to 2 of 2

Thread: How To return C++ Enum to QML?

  1. #1

    Default How To return C++ Enum to QML?

    Hi there,

    I have a use case

    Class B : public QObject
    {
    Q_OBJECT
    Q_ENUMS(Enum)
    public :
    enum Enum { }
    }

    Class A : public QObject
    {
    public :
    B::Enum foo( );
    }

    on QML side:
    var ret = a.foo( );
    console.log(ret) // print out QVariant(B::Enum)
    if ( ret == B.success ) // success is one of enum value; always failed if returned value is success.

    But if I use Q_PROPERTY( B::Enum lastError READ lastError CONSTANT ); it works fine.

    So my question is that Enum can't be return type of a method/a slot from C++ to QML?

  2. #2
    Join Date
    Jul 2011
    Location
    Pune, India
    Posts
    21
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How To return C++ Enum to QML?

    I guess you need to access Enum declared in your cpp code from yout QML code.

    Q_ENUMS(Enum)
    public :
    enum Enum { }
    you just had let know ur qml that u have such enum in your code.

    But u ought to expose that type to the qml code.

    Q_PROPERTY( B::Enum lastError READ lastError CONSTANT )
    what you did maynot be the exact solution.
    add below in your cpp code.

    qmlRegisterType(const char * uri, int versionMajor, int versionMinor, const char * qmlName)

    In your case
    qmlRegisterType<Enum>("Enum", 1, 0, "Enum");

    You should be able to directly access Enum::Whatever_value.

    Check and let me know.

Similar Threads

  1. Replies: 1
    Last Post: 2nd January 2013, 10:48
  2. How to export enum ?
    By Peppy in forum Qt Programming
    Replies: 1
    Last Post: 14th May 2010, 16:15
  3. How can we convert enum to int?
    By learning_qt in forum Qt Programming
    Replies: 2
    Last Post: 26th January 2010, 17:08
  4. enum property
    By illuzioner in forum Qt Tools
    Replies: 10
    Last Post: 22nd August 2006, 22:47
  5. enum scope
    By illuzioner in forum General Programming
    Replies: 1
    Last Post: 15th February 2006, 06:39

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.