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. Usings enums as the return type of a function, does not work in qml
Forum Updated to NodeBB v4.3 + New Features

Usings enums as the return type of a function, does not work in qml

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 2 Posters 853 Views 2 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.
  • C Offline
    C Offline
    Creaperdown
    wrote on last edited by
    #1

    Hey, I am trying to return an enum from my C++ code, my enum is defined in a namespace, and looks like this:

    // book_operation_status.hpp 
    
    namespace application
    { 
    Q_NAMESPACE
    
    enum class BookOperationStatus
    {
        Success,
        ...
    };
    
    Q_ENUM_NS(BookOperationStatus)
    
    } // namespace application
    

    I am trying to export it to qml, using (in main.cpp)

    qmlRegisterUncreatableMetaObject(application::staticMetaObject,                                     
    "Librum.enums", 1, 0, "BookOperationStatus",                                      
    "This enum is uncreatable")
    

    When trying to import and use it in qml, it works just fine, I can console.log() any enum value.
    But when trying to return it from a c++ function, called from QML, I get the error:

    qml:134: Error: Unknown method return type: application::BookOperationStatus
    

    Does anyone have an idea why this happens?
    Thanks in advance.

    Also: my QML code, my enum and the c++ function which returns the enum, are all in different binaries (shared libraries). Maybe this is important here.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You probably need to also register "application::BookOperationStatus" as meta object.

      Also, remember that QML does not quite understand enums, if you want to store it in QML you need to use int.

      (Z(:^

      C 1 Reply Last reply
      0
      • sierdzioS sierdzio

        You probably need to also register "application::BookOperationStatus" as meta object.

        Also, remember that QML does not quite understand enums, if you want to store it in QML you need to use int.

        C Offline
        C Offline
        Creaperdown
        wrote on last edited by
        #3

        @sierdzio How exactly would I register the enum as a meta object?

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          I meant Q_DECLARE_METATYPE(application::BookOperationStatus) but it probably does not apply in this case.

          (Z(:^

          1 Reply Last reply
          0

          • Login

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