Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QVector<int> not working as model?
Forum Updated to NodeBB v4.3 + New Features

QVector<int> not working as model?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 870 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tmnku
    wrote on last edited by tmnku
    #1

    Hi,
    I have a simple class that holds a QVector<int> as a Q_PROPERTY and I expose this class to QML using setContextProperty. I want to use the vector as a model inside a Repeater but I noticed that even though my QML code can access the data (I can print it to the console) the repeater does not generate any elements. So my question is: do I have to create my own models using QAbstractItemModel even for basic types like QVector<int> which Qt is capable of converting from c++ to js?
    I then found this example where they put a QStringList in a QVariant so I tried the same with a QList<int> but still it did not work.
    Here is the class which I expose to QML:
    MyClass.h:

    class MyClass : public QObject {
        Q_OBJECT
        Q_PROPERTY(QVariant mydata READ mydata WRITE setMydata NOTIFY
         mydataChanged)
    
    signals:
         void mydataChanged();
    
    public:
        MyClass(QObject* parent = nullptr);
        QVariant mydata();
        void setMydata(QVariant data);
        QList<int> mydata_;
    };
    

    MyClass.cpp

    MyClass::MyClass(QObject* parent) : QObject(parent), mydata_({1,2,3,4}) {
    }
    
    QVariant MyClass::mydata() {
        return QVariant::fromValue(mydata_);
    }
    
    void MyClass::setMydata(QVariant data) {
        mydata_ = data.value<QList<int>>();
        emit mydataChanged();
    }
    

    The QML file simply draws a rectangle for each element in the model (works fine when I replace model data with an integer like model: 5)

    Repeater {
            model: myclass.mydata
            Rectangle {
                x: index*20
                y: 0
                width: 10
                height: 10
                color: "red"
            }
        }
    

    In case I'm doing it all wrong: what would be the fastest way to create a model which only holds integers?
    Thank you very much.

    raven-worxR 1 Reply Last reply
    0
    • T tmnku

      Hi,
      I have a simple class that holds a QVector<int> as a Q_PROPERTY and I expose this class to QML using setContextProperty. I want to use the vector as a model inside a Repeater but I noticed that even though my QML code can access the data (I can print it to the console) the repeater does not generate any elements. So my question is: do I have to create my own models using QAbstractItemModel even for basic types like QVector<int> which Qt is capable of converting from c++ to js?
      I then found this example where they put a QStringList in a QVariant so I tried the same with a QList<int> but still it did not work.
      Here is the class which I expose to QML:
      MyClass.h:

      class MyClass : public QObject {
          Q_OBJECT
          Q_PROPERTY(QVariant mydata READ mydata WRITE setMydata NOTIFY
           mydataChanged)
      
      signals:
           void mydataChanged();
      
      public:
          MyClass(QObject* parent = nullptr);
          QVariant mydata();
          void setMydata(QVariant data);
          QList<int> mydata_;
      };
      

      MyClass.cpp

      MyClass::MyClass(QObject* parent) : QObject(parent), mydata_({1,2,3,4}) {
      }
      
      QVariant MyClass::mydata() {
          return QVariant::fromValue(mydata_);
      }
      
      void MyClass::setMydata(QVariant data) {
          mydata_ = data.value<QList<int>>();
          emit mydataChanged();
      }
      

      The QML file simply draws a rectangle for each element in the model (works fine when I replace model data with an integer like model: 5)

      Repeater {
              model: myclass.mydata
              Rectangle {
                  x: index*20
                  y: 0
                  width: 10
                  height: 10
                  color: "red"
              }
          }
      

      In case I'm doing it all wrong: what would be the fastest way to create a model which only holds integers?
      Thank you very much.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @tmnku
      https://doc.qt.io/qt-5/qtqml-cppintegration-data.html#sequence-type-to-javascript-array

      use the "length" property as model. an access the elements in the delegate component via the "index" variable

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1
      • T Offline
        T Offline
        tmnku
        wrote on last edited by
        #3

        Thank you, that solved my problem!

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved