Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-104392

Q_ENUM_NS without Q_DECLARE_METATYPE doesn't work

    XMLWordPrintable

Details

    Description

      Documentation for Q_ENUM_NS: https://doc.qt.io/qt-5/qobject.html#Q_ENUM_NS tells that:

      > Registered enumerations are automatically registered also to the Qt meta type system,
      > making them known to QMetaType without the need to use Q_DECLARE_METATYPE().

      But code bellow prints:
      ```
      qrc:/main.qml:15: Error: Unknown method parameter type: Data::DType
      ```

      With un-commented Q_DECLARE_METATYPE the error disappears.
      Also with Qt6 (recent dev branch) all works fine without "Q_DECLARE_METATYPE",
      so this is specific only for Qt 5.15.4

      
      #include <QQuickItem>
      
      namespace Data {
         Q_NAMESPACE
         QML_ELEMENT
         enum DType {
           A, B
         };
        Q_ENUM_NS(DType)
      } // namespace Data
      
      //Q_DECLARE_METATYPE(Data::DType)   
      
      class MyItem : public QQuickItem
      {
      private:
          Q_OBJECT
          QML_ELEMENT
       public:
          MyItem();
          Q_INVOKABLE void setDType(Data::DType) {}
      };
      
      import QtQuick 2.15
      import QtQuick.Window 2.15
      import example.mylib 1.0
      
      Window {
          width: 640
          height: 480
          visible: true
          title: qsTr("Hello World")
          
          Component.onCompleted: {
              myItem.setDType(0);
          }
          MyItem {
              id: myItem
          }
      }
      

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            docteam Qt Documentation Team
            evgeniy_dushistov evgeniy_dushistov
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes