C

ListModel Struct

template <typename T> struct 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

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, first declare a struct that describes the data model. Its public fields become the model roles. The struct must be equality-comparable.

struct AlarmData
{
    AlarmData()
        : seconds(0)
        , running(false)
    {}

    AlarmData(int argSeconds, bool argRunning)
        : seconds(argSeconds)
        , running(argRunning)
    {}

    int seconds;
    bool running;
};

inline bool operator==(const AlarmData &lhs, const AlarmData &rhs)
{
    return lhs.seconds == rhs.seconds && lhs.running == rhs.running;
}

Alternatively, the template argument can be a simple type supported by Qt Quick Ultralite (for example, int or Qul::qreal).

Then, declare a struct that derives from Qul::ListModel.

struct AlarmModel : Qul::ListModel<AlarmData>
{
private:
    std::vector<AlarmData, Qul::PlatformInterface::Allocator<AlarmData> > m_data;

public:
    // Implement the ListModel interface
    int count() const QUL_DECL_OVERRIDE { return m_data.size(); }
    AlarmData data(int index) const QUL_DECL_OVERRIDE { return m_data[index]; }

This struct may contain additional functions that the QML application can use to interact with the model:

void togglePause(int index)
{
    m_data[index].running = !m_data[index].running;
    dataChanged(index);
}

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.

Dynamic strings

Passing strings form C++ to QML has some implications on how glyphs are generated by the fontcompiler. It's possible that some characters will not be rendered in the QML by default. For more detailed description see Rendering dynamic strings in QML.

See also Models and Views in Qt Quick Ultralite, C++ Data Models, and Text Rendering and Fonts.

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.