aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJ-P Nurmi <jpnurmi@digia.com>2014-05-27 14:14:52 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-05-29 09:25:44 +0200
commit0306626a4deb8c36b219ba08a68248d3a454b697 (patch)
treec51fe9b5c7d8b89fdf1b105dc01f318a4da34b05 /src
parentb5cab0515bf316d60ec6dca25b699872d4daeb45 (diff)
Fix emission of QQmlListModel::rowsAboutToBeXxx() signals
Call beginInsertRows(), beginMoveRows() and beginRemoveRows() before the change to ensure that rowsAboutToBeInserted(), rowsAboutToBeMoved() and rowsAboutToBeRemoved() get emitted before the change as appropriate. NOTE: This patch solves the problem for the most common use case, when ListModel is used without WorkerScript. QQmlListModelWorkerAgent needs similar changes in order to fix the signals when ListModel is used with WorkerScript (QTBUG-39321). Task-number: QTBUG-39279 Change-Id: Idec5167d70b242f6f7d8b7cff008e130afc62505 Reviewed-by: Alan Alpert <aalpert@blackberry.com>
Diffstat (limited to 'src')
-rw-r--r--src/qml/types/qqmllistmodel.cpp44
-rw-r--r--src/qml/types/qqmllistmodel_p.h3
2 files changed, 43 insertions, 4 deletions
diff --git a/src/qml/types/qqmllistmodel.cpp b/src/qml/types/qqmllistmodel.cpp
index 0056276d52..f57d4a6a58 100644
--- a/src/qml/types/qqmllistmodel.cpp
+++ b/src/qml/types/qqmllistmodel.cpp
@@ -1699,13 +1699,20 @@ void QQmlListModel::emitItemsChanged(int index, int count, const QVector<int> &r
}
}
+void QQmlListModel::emitItemsAboutToBeRemoved(int index, int count)
+{
+ if (count <= 0 || !m_mainThread)
+ return;
+
+ beginRemoveRows(QModelIndex(), index, index + count - 1);
+}
+
void QQmlListModel::emitItemsRemoved(int index, int count)
{
if (count <= 0)
return;
if (m_mainThread) {
- beginRemoveRows(QModelIndex(), index, index + count - 1);
endRemoveRows();
emit countChanged();
} else {
@@ -1716,13 +1723,20 @@ void QQmlListModel::emitItemsRemoved(int index, int count)
}
}
+void QQmlListModel::emitItemsAboutToBeInserted(int index, int count)
+{
+ if (count <= 0 || !m_mainThread)
+ return;
+
+ beginInsertRows(QModelIndex(), index, index + count - 1);
+}
+
void QQmlListModel::emitItemsInserted(int index, int count)
{
if (count <= 0)
return;
if (m_mainThread) {
- beginInsertRows(QModelIndex(), index, index + count - 1);
endInsertRows();
emit countChanged();
} else {
@@ -1731,13 +1745,20 @@ void QQmlListModel::emitItemsInserted(int index, int count)
}
}
+void QQmlListModel::emitItemsAboutToBeMoved(int from, int to, int n)
+{
+ if (n <= 0 || !m_mainThread)
+ return;
+
+ beginMoveRows(QModelIndex(), from, from + n - 1, QModelIndex(), to > from ? to + n : to);
+}
+
void QQmlListModel::emitItemsMoved(int from, int to, int n)
{
if (n <= 0)
return;
if (m_mainThread) {
- beginMoveRows(QModelIndex(), from, from + n - 1, QModelIndex(), to > from ? to + n : to);
endMoveRows();
} else {
int uid = m_dynamicRoles ? getUid() : m_listModel->getUid();
@@ -1877,6 +1898,8 @@ void QQmlListModel::clear()
{
int cleared = count();
+ emitItemsAboutToBeRemoved(0, cleared);
+
if (m_dynamicRoles) {
for (int i=0 ; i < m_modelObjects.count() ; ++i)
delete m_modelObjects[i];
@@ -1909,6 +1932,8 @@ void QQmlListModel::remove(QQmlV4Function *args)
return;
}
+ emitItemsAboutToBeRemoved(index, removeCount);
+
if (m_dynamicRoles) {
for (int i=0 ; i < removeCount ; ++i)
delete m_modelObjects[index+i];
@@ -1957,6 +1982,7 @@ void QQmlListModel::insert(QQmlV4Function *args)
QV4::ScopedObject argObject(scope);
int objectArrayLength = objectArray->getLength();
+ emitItemsAboutToBeInserted(index, objectArrayLength);
for (int i=0 ; i < objectArrayLength ; ++i) {
argObject = objectArray->getIndexed(i);
@@ -1968,6 +1994,8 @@ void QQmlListModel::insert(QQmlV4Function *args)
}
emitItemsInserted(index, objectArrayLength);
} else if (argObject) {
+ emitItemsAboutToBeInserted(index, 1);
+
if (m_dynamicRoles) {
m_modelObjects.insert(index, DynamicRoleModelNode::create(args->engine()->variantMapFromJS(argObject), this));
} else {
@@ -2006,6 +2034,8 @@ void QQmlListModel::move(int from, int to, int n)
return;
}
+ emitItemsAboutToBeMoved(from, to, n);
+
if (m_dynamicRoles) {
int realFrom = from;
@@ -2061,6 +2091,8 @@ void QQmlListModel::append(QQmlV4Function *args)
int objectArrayLength = objectArray->getLength();
int index = count();
+ emitItemsAboutToBeInserted(index, objectArrayLength);
+
for (int i=0 ; i < objectArrayLength ; ++i) {
argObject = objectArray->getIndexed(i);
@@ -2077,9 +2109,12 @@ void QQmlListModel::append(QQmlV4Function *args)
if (m_dynamicRoles) {
index = m_modelObjects.count();
+ emitItemsAboutToBeInserted(index, 1);
m_modelObjects.append(DynamicRoleModelNode::create(args->engine()->variantMapFromJS(argObject), this));
} else {
- index = m_listModel->append(argObject, args->engine());
+ index = m_listModel->elementCount();
+ emitItemsAboutToBeInserted(index, 1);
+ m_listModel->append(argObject, args->engine());
}
emitItemsInserted(index, 1);
@@ -2174,6 +2209,7 @@ void QQmlListModel::set(int index, const QQmlV4Handle &handle)
if (index == count()) {
+ emitItemsAboutToBeInserted(index, 1);
if (m_dynamicRoles) {
m_modelObjects.append(DynamicRoleModelNode::create(engine()->variantMapFromJS(object), this));
diff --git a/src/qml/types/qqmllistmodel_p.h b/src/qml/types/qqmllistmodel_p.h
index 54ed18865f..3d84e14698 100644
--- a/src/qml/types/qqmllistmodel_p.h
+++ b/src/qml/types/qqmllistmodel_p.h
@@ -144,8 +144,11 @@ private:
static QQmlListModel *createWithOwner(QQmlListModel *newOwner);
void emitItemsChanged(int index, int count, const QVector<int> &roles);
+ void emitItemsAboutToBeRemoved(int index, int count);
void emitItemsRemoved(int index, int count);
+ void emitItemsAboutToBeInserted(int index, int count);
void emitItemsInserted(int index, int count);
+ void emitItemsAboutToBeMoved(int from, int to, int n);
void emitItemsMoved(int from, int to, int n);
};