Model Class

(Qul::Model)
Header: #include <Model>
Inherits: Qul::Items::QtObject

Public Functions

virtual ~Model()
virtual int count() const = 0
virtual T get(int i) const = 0

Public Variables

Signal<void (int)> dataChanged
Signal<void ()> modelReset

Detailed Description

Inherit from this class to expose a model to QML

The template parameter must be a simple struct with public field which are the role of the model. Alternatively, the item can be a simple type supported by Qul (eg: int or float).

Example:


  struct MyModelData {
      int field1;
      float field2;
  };

  struct MyModel : Qul::Model<MyModelData> {
      int count() const override {
          return m_data.size();
      };
      MyModelData get(int idx) const override {
          return m_data.at(idx);
      }
      std::vector<TestModelData> m_data;

      // It is also possible to add some functions that can be called from QML or C++
      void add(int f1, float f2) {
          MyModelData data = { f1, f2 };
          m_data.push_back(data);
          modelReset();
      }
  };

When the data changes, one must call the modelReset or dataChanged signals

Member Function Documentation

[virtual] Model::~Model()

Destroys the instance of Model. The destructor is virtual.

[pure virtual] int Model::count() const

Must be re-implemented and return the number of items

[pure virtual] T Model::get(int i) const

return the item in position i

Member Variable Documentation

Signal<void (int)> Model::dataChanged

Emit this signal to tell the view to reload the data for the item At the given index

Signal<void ()> Model::modelReset

Emit this signal to tell the view that the model needs to be reloaded. This must be emitted when the number of rows, is changed