QAbstractListModel with QML

  • Hello guys,

    I'm struggling to get my qml to work with a QAbstractListModel.
    I started using QList<QObject> models. That worked pretty good.
    Except that these don't communicate any changes on the model.

    So I start implementing a QAbstractListModel approach.
    But it isn't going to work.

    Here is my implementation:

    @#include <QObject>
    #include <QAbstractListModel>

    #include "global_define.h"
    #include "elements/clipelement.h"

    class ClipModel : public QAbstractListModel

    enum ClipElementRoles { ObjectRole = Qt::UserRole+1 };
    explicit ClipModel(QObject *parent = 0);
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    void addClip(ClipElement *clip);
    int rowCount(const QModelIndex & parent = QModelIndex()) const;

    ClipList m_clips; //typedef QList<QObject*> ClipList;--


    public slots:


    @#include "clipmodel.h"

    ClipModel::ClipModel(QObject *parent) :
    QHash<int, QByteArray> roles;
    roles[ObjectRole] = "object";

    QVariant ClipModel::data(const QModelIndex &index, int role) const {
    if (!index.isValid())
    return QVariant();

    if (index.row() >= m_clips.size() || index.row() < 0)
        return QVariant();
    if (role == Qt::DisplayRole) {
        return QVariant::fromValue(this->;
    if (role == ObjectRole)
        return QVariant::fromValue(this->;


    void ClipModel::addClip(ClipElement *clip)
    beginInsertRows(QModelIndex(), rowCount(), rowCount());

    int ClipModel::rowCount(const QModelIndex & /* parent */) const
    return m_clips.count();

    ClipModel *model = new ClipModel(this);

    ClipElement *temp = new ClipElement();
    this->viewer.rootContext()->setContextProperty("clip_model", model);


    The first problem with this is an build error saying:
    'setRoleNames' was not declared in this scope
    Ok ? what am I doing wrong here so far ?

    The clipmodel is used by an ListView with the following delegate:
    @Component {
    id: clipDelegate
    Item {
    width: parent.parent.width
    height: 50
    Rectangle {
    anchors.fill: parent
    color: "red"
    border.color: "black"
    Text {
    id: clipText
    anchors.fill: parent
    text: ??? #how to get the properties of the ClipElement ?

    I guess that without setting any role I wouldn't be able to get the properties of the ClipElement object.
    But how am i supposed to set the roles and address the roles in qml ?

    Is there any complete tutorial out there showing the combination of QML witch a QAbstractListModel ?
    I found nothing that helped me here. The Qt documentation is pretty incomplete on this topic.

    Thanks for help.

  • Hi,

    Take a look at

    This code helped me to solve the same issue.

    setRoleNames() method is obsolute, you should override roleNames() instead.

  • hey yudjin,

    thanks for that example. Yesterday I stumbled upon this one .

    Both examples are pretty helpful in this case.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.