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

QML_SEQUENTIAL_CONTAINERS affects isArray and breaks ComboBox

    XMLWordPrintable

Details

    Description

      The attached example feeds a simple QList<Entry>* model to a ComboBox and uses textRole to adjust the display text.

      QML:

      Window {
          Component.onCompleted: () => {
              let tmp = Foo.getEntries()
              console.log("Model is array: " + Array.isArray(tmp))
              selector.model = tmp
          }
      
          ComboBox {
              id: selector
              textRole: 'name'
          }
      } 

      List:

      class Entry : public QObject {
          Q_OBJECT
          Q_PROPERTY(QString name MEMBER m_name CONSTANT FINAL)
      
      public:
          explicit Entry(const QString& name, QObject* parent = nullptr) :
              QObject(parent), m_name(name) {}
      
      private:
          QString m_name;
      };
      
      class EntryWrapper {
          Q_GADGET
          QML_FOREIGN(Entry)
          QML_NAMED_ELEMENT(Entry)
          QML_UNCREATABLE("These are my Entry objects")
      };
      
      class EntryListRegistration
      {
          Q_GADGET
          QML_FOREIGN(QList<Entry*>)
          QML_ANONYMOUS
          QML_SEQUENTIAL_CONTAINER(Entry*)
      };
      
      class Foo : public QObject {
          Q_OBJECT
          QML_ELEMENT
          QML_SINGLETON
      
      public:
          explicit Foo(QObject* parent = nullptr) : QObject(parent) {
              for (int i = 0; i < 10; i++) {
                  m_entries.push_back(new Entry(QString("Item %1").arg(i), this));
              }
          }    Q_INVOKABLE QList<Entry*> getEntries() const { return m_entries; }
      
      private:
          QList<Entry*> m_entries;
      };
      

       
      The resulting ComboBox shows the selected entry, but when opening it, it shows invisible entries. This is because isArray is false for the model. Removing the list registration makes it work.

      Attachments

        Activity

          People

            fabiankosmale Fabian Kosmale
            cajus Cajus Pollmeier
            Votes:
            2 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

              Created:
              Updated: