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

Assigning QVector<QString> to Repeater's model property results in count being 0

    XMLWordPrintable

Details

    • d621027babff9a30d56ab6af871a465108c9eaba

    Description

      https://doc.qt.io/qt-5/qtqml-cppintegration-data.html#sequence-type-to-javascript-array says that QVector<QString> can be converted to a JavaScript array. QTBUG-67459 was closed as a duplicate of QTBUG-60134, which was supposedly fixed by https://codereview.qt-project.org/c/qt/qtdeclarative/+/191873. However, this example shows that the Repeater's count is 0 when such a model is assigned (despite printing the array showing that there is one item):

      main.cpp
      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      #include <QAbstractListModel>
      #include <QQmlContext>
      #include <QKeyEvent>
      #include <QDebug>
      #include <QAbstractListModel>
      
      class ItemData : public QObject
      {
          Q_OBJECT
          Q_PROPERTY(QVector<QString> roundNames READ roundNamesVector CONSTANT FINAL)
      
      public:
          ItemData(QObject *parent = nullptr) : QObject(parent) {}
      
          QVector<QString> roundNamesVector() const {
              return QVector<QString>(mRoundNames.constBegin(), mRoundNames.constEnd());
          }
      
          QSet<QString> mRoundNames;
      };
      
      int main(int argc, char *argv[])
      {
          QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
      
          QGuiApplication app(argc, argv);
      
          QQmlApplicationEngine engine;
      
          qmlRegisterAnonymousType<ItemData>("App", 1);
      
          const QUrl url(QStringLiteral("qrc:/main.qml"));
          QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                           &app, [url](QObject *obj, const QUrl &objUrl) {
              if (!obj && url == objUrl)
                  QCoreApplication::exit(-1);
          }, Qt::QueuedConnection);
          engine.load(url);
      
          ItemData itemData;
          itemData.mRoundNames.insert(QLatin1String("Test"));
          engine.rootObjects().first()->setProperty("itemData", QVariant::fromValue(&itemData));
      
          return app.exec();
      }
      
      #include "main.moc"
      
      main.qml
      import QtQuick 2.14
      import QtQuick.Window 2.14
      
      Window {
          id: root
          width: 300
          height: 480
          title: "count: " + repeater.count + " roundNames: " + (itemData ? itemData.roundNames : [])
          visible: true
      
          property var itemData
      
          Flow {
              anchors.fill: parent
      
              Repeater {
                  id: repeater
                  model: root.itemData ? root.itemData.roundNames : null
                  delegate: Text {
                      text: modelData
                  }
              }
          }
      }
      

      Attachments

        Issue Links

          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
              mitch_curtis Mitch Curtis
              Votes:
              2 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes