C

ListModel Class

template <typename T> class Qul::ListModel

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

Header: #include <ListModel>
Inherits: Qul::Object

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

Detailed Description

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 qreal).

Example:

struct MyModelData {
    int field1;
    qreal field2;
};

struct MyModel : Qul::ListModel<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, qreal 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

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

Must be re-implemented and return the number of items

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

Returns the item at index.

T ListModel::get(int index) const

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

See also data.

Member Variable Documentation

Signal<void (int)> ListModel::dataChanged

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

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

Available under certain Qt licenses.
Find out more.