Hey Guys,

I am already struggling with this problem for about 3 days... seems I am kinda stuck now =/.
What I want: I want a QList<T> to interact with my QML view - dynamically - so if I remove an item via a C++ interface call to the model the item should disappear in the QML view

What I know so far: I recognized (logical anyways) that the QList i had before:
Qt Code:
  1. //Q_PROPERTY(QDeclarativeListProperty<TaskData> taskData READ taskData CONSTANT)
  2. //QDeclarativeListProperty<TaskData> taskData();
To copy to clipboard, switch view to plain text mode 
wasn't working because it provides no notification for the view -> after some research I decided to implement my own ListModel then...

The Model seems to work but the View does not display anything...

QMLPtrNotificationModel.h looks like shown below:
Qt Code:
  1. class QMLPtrAbstractItem : public QDeclarativeItem
  2. {
  3. public:
  4. QMLPtrAbstractItem(QDeclarativeItem *parent = 0);
  5. virtual ~QMLPtrAbstractItem() { }
  6.  
  7. virtual QString getName() const = 0;
  8. virtual QString setName() const = 0;
  9. };
  10.  
  11. class QMLPtrNotificationModel : public QAbstractListModel
  12. {
  13. Q_OBJECT
  14.  
  15. public:
  16. explicit QMLPtrNotificationModel(QObject *parent = 0);
  17. QMLPtrNotificationModel(const QList<QMLPtrAbstractItem*> &list, QObject *parent = 0);
  18. ~QMLPtrNotificationModel();
  19.  
  20. int rowCount(const QModelIndex &parent = QModelIndex()) const;
  21. int count() const { return rowCount(QModelIndex()); }
  22.  
  23. QVariant data(const QModelIndex &index, int role) const;
  24. bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
  25.  
  26. Qt::ItemFlags flags(const QModelIndex &index) const;
  27.  
  28. bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex());
  29. bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
  30.  
  31. void sort(int column, Qt::SortOrder order = Qt::AscendingOrder);
  32.  
  33. QList<QMLPtrAbstractItem*> operator()();
  34. const QList<QMLPtrAbstractItem*> operator()();
  35.  
  36. QList<QMLPtrAbstractItem*> getList() const;
  37. void setList(const QList<QMLPtrAbstractItem*> &list);
  38.  
  39. QMLPtrAbstractItem* at(int index);
  40.  
  41. bool addElement(QMLPtrAbstractItem *element);
  42. bool removeElement(QMLPtrAbstractItem *element);
  43. bool clear();
  44.  
  45. Qt::DropActions supportedDropActions() const;
  46.  
  47. private Q_SLOTS:
  48. void refresh();
  49.  
  50. private:
  51. Q_DISABLE_COPY(QMLPtrNotificationModel)
  52. QList<QMLPtrAbstractItem*> lst;
  53. };
  54.  
  55. QML_DECLARE_TYPE(QMLPtrNotificationModel)
To copy to clipboard, switch view to plain text mode 


The QMLPtrNotificationModel.cpp looks like shown below (not all functions provided - I think that not all are necessary to document the problem)
Qt Code:
  1. QMLPtrAbstractItem::QMLPtrAbstractItem(QDeclarativeItem *parent)
  2. : QDeclarativeItem(parent)
  3. {
  4. }
  5.  
  6. QMLPtrNotificationModel::QMLPtrNotificationModel(QObject *parent)
  7. {
  8. }
  9.  
  10. QMLPtrNotificationModel::QMLPtrNotificationModel(const QList<QMLPtrAbstractItem*> &list, QObject *parent)
  11. : QAbstractListModel(parent), lst(list)
  12. {
  13. }
  14.  
  15. QMLPtrNotificationModel::~QMLPtrNotificationModel()
  16. {
  17. clear();
  18. }
  19.  
  20. int QMLPtrNotificationModel::rowCount(const QModelIndex &parent) const
  21. {
  22. if (parent.isValid())
  23. return 0;
  24.  
  25. return lst.count();
  26. }
  27.  
  28. QVariant QMLPtrNotificationModel::data(const QModelIndex &index, int role) const
  29. {
  30. if (index.row() < 0 || index.row() >= lst.size())
  31. return QVariant();
  32.  
  33. if (role == Qt::DisplayRole || role == Qt::EditRole)
  34. return lst.at(index.row())->getName();
  35.  
  36. return QVariant();
  37. }
  38.  
  39. Qt::ItemFlags QMLPtrNotificationModel::flags(const QModelIndex &index) const
  40. {
  41. if (!index.isValid())
  42. return QAbstractItemModel::flags(index) | Qt::ItemIsDropEnabled;
  43.  
  44. return QAbstractItemModel::flags(index) | Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
  45. }
  46.  
  47. bool QMLPtrNotificationModel::setData(const QModelIndex &index, const QVariant &value, int role)
  48. {
  49. if (index.row() >= 0 && index.row() < lst.size()
  50. && (role == Qt::EditRole || role == Qt::DisplayRole)) {
  51.  
  52. lst.at(index.row())->setName(value.toString());
  53. emit dataChanged(index, index);
  54. return true;
  55. }
  56. return false;
  57. }
  58.  
  59. bool QMLPtrNotificationModel::insertRows(int row, int count, const QModelIndex &parent)
  60. {
  61. if (count < 1 || row < 0 || row > rowCount(parent))
  62. return false;
  63.  
  64. beginInsertRows(QModelIndex(), row, row + count - 1);
  65.  
  66. for (int r = 0; r < count; ++r){
  67. lst.insert(row, NULL);
  68. }
  69.  
  70. endInsertRows();
  71.  
  72. return true;
  73. }
  74.  
  75. bool QMLPtrNotificationModel::removeRows(int row, int count, const QModelIndex &parent)
  76. {
  77. if (count <= 0 || row < 0 || (row + count) > rowCount(parent))
  78. return false;
  79.  
  80. beginRemoveRows(QModelIndex(), row, row + count - 1);
  81.  
  82. for (int r = 0; r < count; ++r){
  83. delete lst.at(row);
  84. lst.removeAt(row);
  85. }
  86.  
  87. endRemoveRows();
  88.  
  89. return true;
  90. }
  91.  
  92. bool QMLPtrNotificationModel::clear()
  93. {
  94. return removeRows(0, rowCount(), QModelIndex());
  95. }
  96.  
  97. bool QMLPtrNotificationModel::addElement(QMLPtrAbstractItem *element)
  98. {
  99. int position = rowCount();
  100. if(insertRows(position, 1, QModelIndex())){
  101. lst << element;
  102. return true;
  103. }
  104. return false;
  105. }
  106.  
  107. bool QMLPtrNotificationModel::removeElement(QMLPtrAbstractItem *element)
  108. {
  109. int position = lst.indexOf(element);
  110. if(removeRows(position, 1, QModelIndex())){
  111. return true;
  112. }
  113. return false;
  114. }
  115.  
  116. QMLPtrAbstractItem* QMLPtrNotificationModel::at(int index)
  117. {
  118. return lst.at(index);
  119. }
To copy to clipboard, switch view to plain text mode 

It seems QML doesn't recognize anthing - the structure looks as follows:
Qt Code:
  1. import QtQuick 1.1
  2. import QMLPtrNotificationModel 1.0
  3.  
  4. Item {
  5. property variant orientation: Qt.Vertical
  6.  
  7. ListView {
  8. id: completeTable
  9.  
  10. QMLPtrNotificationModel {
  11. id: myListmodel
  12. }
  13.  
  14. model: myListmodel
  15. delegate: TaskData { }
  16. }
To copy to clipboard, switch view to plain text mode 

And i registered QMLPtrNotificationModel as QML Type via C++ like that:
Qt Code:
  1. qmlRegisterType<QMLPtrNotificationModel>("QMLPtrNotificationModel", 1, 0, "QMLPtrNotificationModel");
To copy to clipboard, switch view to plain text mode 

I know it's a big part of code for a single post - but I think the error is in both parts, QML and the Model itself.. looks like missing notifications again?
Would be really lovely if anybody of you has an idea to fix this issue -> my QML view is empty at the moment (I added elements via addElement())
The Elements to add are correctly derived from QMLPtrAbstractItem and implement the abstract methods.

Any ideas?