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:
clipmodel.h
@#include <QObject>
#include <QAbstractListModel>#include "global_define.h"
#include "elements/clipelement.h"class ClipModel : public QAbstractListModel
{
Q_OBJECT
public: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;
private:
ClipList m_clips; //typedef QList<QObject*> ClipList;--signals:
public slots:
};@
clipmodel.cpp
@#include "clipmodel.h"ClipModel::ClipModel(QObject *parent) :
QAbstractListModel(parent)
{
QHash<int, QByteArray> roles;
roles[ObjectRole] = "object";
setRoleNames(roles);
}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->m_clips.at(index.row())); } if (role == ObjectRole) return QVariant::fromValue(this->m_clips.at(index.row()));
}
void ClipModel::addClip(ClipElement *clip)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_clips.append(clip);
endInsertRows();
}int ClipModel::rowCount(const QModelIndex & /* parent */) const
{
return m_clips.count();
}@@
ClipModel *model = new ClipModel(this);ClipElement *temp = new ClipElement(); temp->setClipID("foo"); temp->setClipStatus("bar"); model->addClip(temp); 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 {
id:clipContainer
width: parent.parent.width
height: 50
Rectangle {
id:clipBackground
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 https://github.com/motya/testsqlqml/blob/83c281ebd536b15bb70f49a16a6ae85d09ba4544/mytablemodel.cpp#L38
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 https://qt.gitorious.org/qt/qtdeclarative/source/87d0c02fab03b7d3bf2094af22465f029bcdd096:examples/quick/models/abstractitemmodel .
Both examples are pretty helpful in this case.