Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QAbstractListModel with QML
Qt 6.11 is out! See what's new in the release blog

QAbstractListModel with QML

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 7.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved