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. Exposing already defined enum to QML
Forum Updated to NodeBB v4.3 + New Features

Exposing already defined enum to QML

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

    I have an enum which is already define as below:

    // Defines.h file
    namespace com {
    enum class BookType {
    SMALL = 0, BIG, UNKOWN
    };
    }
    
    Above enum doesn't have a Q_NAMESPACE declared.
    I created the QObject class to expose enum to QML
    
    // Book.h file
    #include "Defines.h"
    namespace com {
    Q_NAMESPACE
    Q_ENUM_NS(BookType)
    }
    
    namespace ui {
    class Book: public QObject {
    Q_OBJECT
    QML_ELEMENT
    QML_SINGLETON
    Q_PROPERTY(com::BookType type READ type WRITE setType NOTIFY onTypeChanged)
    ....
    }
    }
    
    // Main.qml
    
    Button {
    onClicked: {
    console.log(Book.BookType.BIG) // gives undefined
    console.log(Book.type) // gives 1
    }
    }
    

    How can access BookType enum be used in qml ?

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

      What about:

      console.log(BookType.BIG) 
      

      (Z(:^

      J 1 Reply Last reply
      0
      • sierdzioS sierdzio

        What about:

        console.log(BookType.BIG) 
        
        J Offline
        J Offline
        jay1
        wrote on last edited by
        #3

        @sierdzio It prints following errors

        ReferenceError: BookType is not defined
        TypeError: Cannot read property 'BIG' of undefined
        
        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          An easy but ugly way would be to change com into a stuct with Q_GADGET.

          Alternatively you should be able to register the enum manually Qt5-way:

          qRegisterMetaType<com::BookType>();
          

          (Z(:^

          1 Reply Last reply
          0
          • TomZT Offline
            TomZT Offline
            TomZ
            wrote on last edited by
            #5

            enums are always a mess, basically the general idea to have a clean namespace with enums doesn't work with QML.

            I'm always just going for the slightly ugly but entirely functional solution to make the namespace a QObject instead. Example:
            https://codeberg.org/Flowee/pay/src/branch/master/src/WalletEnums.h

            Then in your C++ you just need: qmlRegisterType<WalletEnums>("something.or.other", 1, 0, "MyEnums");

            Then in your QML you need to import the first argument (something.or.other) and you can use your values in that file without problems.

            GrecKoG 1 Reply Last reply
            0
            • J Offline
              J Offline
              jay1
              wrote on last edited by
              #6

              I ended up creating the enum and registering it as a type.

              1 Reply Last reply
              0
              • TomZT TomZ

                enums are always a mess, basically the general idea to have a clean namespace with enums doesn't work with QML.

                I'm always just going for the slightly ugly but entirely functional solution to make the namespace a QObject instead. Example:
                https://codeberg.org/Flowee/pay/src/branch/master/src/WalletEnums.h

                Then in your C++ you just need: qmlRegisterType<WalletEnums>("something.or.other", 1, 0, "MyEnums");

                Then in your QML you need to import the first argument (something.or.other) and you can use your values in that file without problems.

                GrecKoG Offline
                GrecKoG Offline
                GrecKo
                Qt Champions 2018
                wrote on last edited by
                #7

                @TomZ said in Exposing already defined enum to QML:

                enums are always a mess, basically the general idea to have a clean namespace with enums doesn't work with QML.

                It does work fine with namespaces. Q_ENUM needs to be made in the scope where the enum was defined though, it doesn't work with an already defined enum. And this requirement exists for both object/gadgets and namespaces.
                The code you linked would have been fine with a namespace.

                Also don't use qmlRegister*** in Qt 6, QML_*** are preferred.

                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