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

Using custom C++ enum flags in QML is not documented well enough

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P4: Low
    • None
    • 5.5.1
    • Documentation
    • None

    Description

      http://doc.qt.io/qt-5/qtqml-cppintegration-data.html

      Suppose you had a property that you wanted to be a combination of enum flags:

      Q_PROPERTY(ClosePolicy closePolicy READ closePolicy WRITE setClosePolicy NOTIFY closePolicyChanged FINAL)
      

      You would declare the enum like so, using Q_DECLARE_FLAGS and Q_FLAG:

          enum ClosePolicyType {
              DontCloseOnEvents = 0x0,
              CloseOnPress = 0x1,
              CloseOnRelease = 0x2,
              CloseOnEscape = 0x4
          };
          Q_DECLARE_FLAGS(ClosePolicy, ClosePolicyType)
          Q_FLAG(ClosePolicy)
      

      You'd then register the containing type:

      qmlRegisterType<QQuickPopup>(uri, 1, 0, "Popup");
      

      And try to use it:

      Popup {
          closePolicy: Popup.DontCloseOnEvents
      }
      

      You'll get an error. Something like "cannot assign int to unknown property type".

      You have to use QML_DECLARE_TYPE:

      QML_DECLARE_TYPE(QQuickPopup)
      

      I would suggest adding a line to http://doc.qt.io/qt-5/qqmlengine.html#QML_DECLARE_TYPE that says something like:

      This macro must be used for types that declare enum flags that are to be used from QML.

      Also, http://doc.qt.io/qt-5/qtqml-cppintegration-data.html#enumeration-types should mention (maybe in a "Flags" sub header) how to properly declare enum flags in C++ in order to use them in this manner in QML.

      Attachments

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

        Activity

          People

            docteam Qt Documentation Team
            mitch_curtis Mitch Curtis
            Votes:
            3 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes