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

QML "composite type" based on a C++ type does not pass through the Q_ENUMS declared

    XMLWordPrintable

Details

    Description

      class QQuickAbstractMessageDialog : public QQuickAbstractDialog
      {
          Q_OBJECT
          Q_ENUMS(Icon)
          Q_PROPERTY(Icon icon READ icon WRITE setIcon NOTIFY iconChanged)
      
      public:
          enum Icon { NoIcon, Information, Warning, Question };
      ...
      }
      
      class QQuickQMessageBox : public QQuickAbstractMessageDialog
      ... implementation using QMessageBox ...
      
      qmlRegisterType<QQuickQMessageBox>(uri, 1, 1, "QtMessageDialog");
      

      WidgetMessageDialog.qml:

      import QtQuick 2.1
      import QtQuick.PrivateWidgets 1.1
      
      QtMessageDialog { }
      

      qmlRegisterType("file:///path/to/WidgetMessageDialog.qml", "QtQuick.Dialogs", 1, 1, "MessageDialog");

      Now finally the test QML:

      MessageDialog {
          icon: MessageDialog.Warning
      }
      

      MessageDialogs.qml:59:15: Unable to assign [undefined] to int

      The reason is that MessageDialog is a "composite type". Even though it's based on a C++ class, the Q_ENUMS from the C++ class are not inherited inside MessageDialog. I can work around it by registering the abstract C++ type too:

      qmlRegisterType<QQuickAbstractMessageDialog>(..., "AbstractMessageDialog")

      then AbstractMessageDialog.Warning will exist and can be used in QML. But this breaks encapsulation so is not as nice as MessageDialog.Warning.

      I can't use an alias either, but maybe we need a similar concept to be able to alias an enum in a QML type; or else inherit all the enums automatically so I don't have to do that.

      As an alternative workaround I might have to register the enums in the QtQuick.Dialogs namespace or something like that (the way that Qt.xxx is a catchall for a lot of enums).

      Attachments

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

        Activity

          People

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            srutledg Shawn Rutledge
            Votes:
            3 Vote for this issue
            Watchers:
            9 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes