Commit d375da53 authored by Thomas BOUTROUE's avatar Thomas BOUTROUE

Improve QQmlVariantListModel

parent bbeecfe0
......@@ -6,6 +6,7 @@
#include <QHash>
#include <QList>
#include <QAbstractListModel>
#include <QDebug>
class QQmlVariantListModelBase : public QAbstractListModel {
Q_OBJECT
......@@ -31,6 +32,10 @@ public:
: QQmlVariantListModelBase { parent }
, m_items { list }
{ }
explicit QQmlVariantListModel (const QList<T> & list, QObject * parent = nullptr)
: QQmlVariantListModelBase { parent }
, m_items { list }
{ }
explicit QQmlVariantListModel (QObject * parent = nullptr)
: QQmlVariantListModel { { }, parent }
{ }
......@@ -61,7 +66,7 @@ public:
index.row () < m_items.count () &&
role == Qt::UserRole) {
m_items.replace (index.row (), data.value<T> ());
emit dataChanged (index, index, QVector<int> { role });
emit dataChanged (index, index, QVector<int> { });
ret = true;
}
return ret;
......@@ -96,6 +101,16 @@ public:
endInsertRows ();
emit countChanged ();
}
void append (const QList<T> & itemsList) {
if (!itemsList.isEmpty ()) {
beginInsertRows (ROOT (), m_items.count (), m_items.count () + itemsList.count () -1);
for (const T & item : itemsList) {
m_items.append (item);
}
endInsertRows ();
emit countChanged ();
}
}
void prepend (const T & item) {
beginInsertRows (ROOT (), 0, 0);
m_items.prepend (item);
......@@ -110,6 +125,12 @@ public:
emit countChanged ();
}
}
void replace (const int idx, const T & item) {
if (idx >= 0 && idx <= m_items.count ()) {
m_items.replace (idx, item);
emit dataChanged (index (idx), index (idx), QVector<int> { });
}
}
void remove (const int idx) {
if (idx >= 0 && idx < m_items.count ()) {
beginRemoveRows (ROOT (), idx, idx);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment