ListModel Struct

template <typename T> struct Qul ::ListModel

Inherit from this class to expose a model to QML. 更多...

Header: #include <qul/model.h>
Since: Qt Quick Ultralite 1.0
实例化: ListModel
继承: Qul::Object

This struct was introduced in Qt Quick Ultralite 1.0.


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


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


const int StaticCount


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
    int seconds = 0;
    bool running = false;
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>
    std::vector<AlarmData> m_data;
    // Implement the ListModel interface
    int count() const override { return m_data.size(); }
    AlarmData data(int index) const 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;

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

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

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 .

另请参阅 Models and Views in Qt Quick Ultralite , C++ Data Models ,和 文本渲染和字体 .


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

Returns the number of data entries in the model.

另请参阅 StaticCount .

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

Returns the data stored at the given index .

T ListModel:: get ( int index ) const

返回项在 index . Exists for QML interface compatibility.

另请参阅 data .


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.

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

另请参阅 count .

信号 < void ( int )> ListModel:: dataChanged

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

另请参阅 modelReset .

信号 < 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

另请参阅 dataChanged .