[Qt-qml] emit data on a QAbstracListModel does not update the QML ListView.

Stephen Kelly steveire at gmail.com
Tue Sep 14 15:58:54 CEST 2010


Bartosh Wroblevksy wrote:

> 
> Hi,
> 
> I have an example in attachment and don't see what I am doing wrong.
> Essentially, in this example at least, I emit dataChanged in a model in a
> cpp file and the  QML ListView hooked up  to it refuses to update. the qml
> file is LibraryList.qmlthe model is in model.cpp What am I doing wrong?
> Thanks, Bartosh

If you move things, you need to communcate that you are moving things, not 
that you are changing data. You do that by either using 
beginMoveRows/endMoveRows or with layoutAboutToBeChanged/layoutChanged 
*plus* updating persistent indexes in between.

$ git diff
diff --git a/model.cpp b/model.cpp
index 3c20b7f..f6750eb 100644
--- a/model.cpp
+++ b/model.cpp
@@ -52,11 +52,13 @@ QVariant Model::data(const QModelIndex &index, int role 
) const
 
 void Model::move()
 {
+    beginMoveRows(QModelIndex(), 0, 0, QModelIndex(), 2);
     m_elements.move(0,1);
+    endMoveRows();
 
     qDebug() << m_elements;
 
-    emit dataChanged(index(0,0), index(1,0));
+//     emit dataChanged(index(0,0), index(1,0));
 
 }
 
Unfortunately there is a bug in the qml handling of moves. so the above 
doesn't work correctly. I'll file a BR.

What will work though is using beginResetModel and endResetModel.

All the best,

Steve.



More information about the Qt-qml mailing list