ListModel Class

template <typename T> class Qul::ListModel

Inherit from this class to expose a model to QML. More...

Header: #include <qul/model.h>
Since: Qt Quick Ultralite 1.0
Instantiated By: ListModel
Inherits: Qul::Object

This class was introduced in Qt Quick Ultralite 1.0.

Public Functions

virtual int count() const = 0
virtual T data(int index) const = 0
T get(int index) const

Public Variables

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

Static Public Members

const int StaticCount

Detailed Description

Using C++ models is useful for exposing existing C++ data models or other complex datasets to QML. Creating a model in C++ is the only way to have a non-readonly model: The ones defined in QML cannot be modified at runtime.

To create your own model, declare a class that derives from Qul::ListModel. Use a struct describing the model data as the template argument. Its public fields become the model roles. Alternatively, the template argument can be a simple type supported by Qul (for example, int or qreal).


struct MyModelData {
    int field1;
    qreal field2;

struct MyModel : Qul::ListModel<MyModelData> {
    Model() {
        m_data.push_back({ 1, 2.0 });

    int count() const override {
        return m_data.size();
    MyModelData data(int index) const override {
        return m_data.at(index);
    std::vector<MyModelData> m_data;

    // It is also possible to add some functions that can be called from QML or C++
    void add(int f1, qreal f2) {
        MyModelData data = { f1, f2 };
    void updateField1(int index, int value) {
        m_data.at(index).field1 = value;

The modelReset and dataChanged signals are used to trigger a refresh of a view.

The modelReset signal must be emitted whenever number of elements of the model changes after it's instantiation by a view.

The dataChanged signal must be emitted whenever existing element of the model is modified.

Member Function Documentation

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

Returns the number of data entries in the model.

See also StaticCount.

[pure virtual] T ListModel::data(int index) const

Returns the data stored at the given index.

T ListModel::get(int index) const

Returns the item at index. Exists for QML interface compatibility.

See also data.

Member Variable Documentation

const int ListModel::StaticCount

The static member variable indicating the exact number of elements in the model, it must be overriden for the model to be used by a Repeater.

Note: This variable must be overridden by defining a static const int StaticCount the derived class.

See also count.

Signal<void (int)> ListModel::dataChanged

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

See also modelReset.

Signal<void ()> ListModel::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

See also dataChanged.

Available under certain Qt licenses.
Find out more.