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

[REG 5.15.2->6.2] DelegateChooser not finding choice

XMLWordPrintable

    • macOS
    • d00c76171de0c76794eb70a7284326332c0b3c66 (qt/qtdeclarative/dev) 159dfd19a742276a1f8f610d46a2a3e2ed3d99f6 (qt/qtdeclarative/6.2)

      DelegateChooser doesn't find the role choice. This problem happens in Qt6.x (tested in Qt6.2) but the same code works in Qt5.15.2.

      I tried to make a simple example to show the issue, but it happens only in combination with some specific elements (Q_ENUM_NS, QQmlListProperty, so on). Depending on what you change, the problem stops happening.

      Here is some code snippets of what is involved to reproduce. (Or check the full project attached).

      Main.qml

      import QtQuick 2.15
      import QtQuick.Controls 2.15
      import QtQuick.Layouts 1.15
      import QtQuick.Window  2.15
      
      import Qt.labs.qmlmodels 1.0
      import test.chooser 1.0
      
      ApplicationWindow {
          width: 600
          height: 400
      
          visible: true
      
          ColumnLayout
          {
            anchors.centerIn: parent
      
            width: 200
            height: 200
      
            Repeater {
              model: Items.all
      
              DelegateChooser {
                role: "type"
      
                DelegateChoice {
                  roleValue: EnumNamespace.LABEL_TYPE
      
                  Text {
                    text: modelData.name
                    font.pixelSize: 20
                    horizontalAlignment: Text.AlignHCenter
                  }
                }
      
                DelegateChoice {
                  roleValue: EnumNamespace.FIELD_TYPE
      
                  TextField {
                    placeholderText: modelData.name
                    font.pixelSize: 20
                    horizontalAlignment: Text.AlignHCenter
                  }
      
                }
              }
            }
          }
      }

       

      Items.hpp

      ...
      namespace EnumNamespace
      {
      Q_NAMESPACE enum ItemType
      {
        LABEL_TYPE = 0,
        FIELD_TYPE = 1
      };
      Q_ENUM_NS(ItemType)
      }
      
      class Item : public QObject {
          Q_OBJECT
      
          Q_PROPERTY(EnumNamespace::ItemType type MEMBER mType CONSTANT)
          Q_PROPERTY(QString name MEMBER mName CONSTANT)
      
      public:
          Item (EnumNamespace::ItemType _type, QString _name) : mType(_type), mName(_name) {}
          Item (const Item& other) : mType(other.mType), mName(other.mName) {}
      
          EnumNamespace::ItemType mType;
          QString mName;
      };
      
      class Items : public QObject {
          Q_OBJECT
      
          Q_PROPERTY(QQmlListProperty<Item> all READ getAll CONSTANT)
      
      // This is only difference of the code depending on the Qt version as the QQmlListProperty API was changed in Qt6.x
      #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
          using size_type = int;
      #else
          using size_type = qsizetype;
      #endif
      
      public:
          Items() {
              data.emplace_back(EnumNamespace::LABEL_TYPE, "Label 1");
              data.emplace_back(EnumNamespace::LABEL_TYPE, "Label 2");
              data.emplace_back(EnumNamespace::FIELD_TYPE, "Edit here...");
          }
      
      private:
          QQmlListProperty<Item> getAll() {
              return QQmlListProperty<Item>(
                  this, this,
                  [](QQmlListProperty<Item>* list) {
                    if (auto* me = qobject_cast<Items*>(list->object))
                    {
                      return static_cast<size_type>(me->data.size());
                    }
      
                    return size_type{0};
                  },
                  [](QQmlListProperty<Item>* list, size_type at) -> Item* {
                    if (auto* me = qobject_cast<Items*>(list->object))
                    {
                      const auto position = static_cast<size_t>(at);
                      return me->data.size() > position ? &me->data[position] : nullptr;
                    }
                    return nullptr;
                  });
          }
      
          std::vector<Item> data;
      };

       

      Main.cpp

      {
          Items items;
      ... 
          qmlRegisterSingletonInstance<Items>("test.chooser", 1, 0, "Items", &items);
          qmlRegisterUncreatableType<Item>("test.chooser", 1, 0, "Item", "");
          qmlRegisterUncreatableMetaObject(EnumNamespace::staticMetaObject, "test.chooser", 1, 0, "EnumNamespace", "Enum cannot be created");
      ...
      }

       

      Here is the result using with 5.15.2: (everything as expected)

      Here is the result using with 6.2: (not showing the controls because the choice of the chooser was not located)

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

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            gbkoerich George Broering Koerich
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes