Blame SOURCES/0008-QQmlDelegateModel-Refresh-the-view-when-a-column-is-.patch

72e842
From 4fd8f31beb6ce07207cc8e934c7f4caa6abd8a54 Mon Sep 17 00:00:00 2001
72ccf7
From: Aleix Pol <aleixpol@kde.org>
72ccf7
Date: Thu, 23 Sep 2021 03:43:04 +0200
72e842
Subject: [PATCH 08/20] QQmlDelegateModel: Refresh the view when a column is
72ccf7
 added at 0
72ccf7
72ccf7
It can happen that a model reports n>0 rows but columns=0 (See
72ccf7
QConcatenateTablesProxyModel). In those cases we would render glitchy
72ccf7
items until the elements are marked as dirty.
72ccf7
72ccf7
Change-Id: I615c9cacbb1b6f9dee3898b03476605e5ac39d0a
72ccf7
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
72ccf7
(cherry picked from commit ec9251efb918f37971aeefa1f687d137d037ff12)
72ccf7
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
72ccf7
Signed-off-by: Aleix Pol <aleixpol@kde.org>
72ccf7
---
72ccf7
 src/qmlmodels/qqmldelegatemodel.cpp           | 44 +++++++++++++++++++
72ccf7
 src/qmlmodels/qqmldelegatemodel_p.h           |  3 ++
72ccf7
 .../data/redrawUponColumnChange.qml           | 11 +++++
72ccf7
 .../qqmldelegatemodel/qqmldelegatemodel.pro   |  2 +-
72ccf7
 .../tst_qqmldelegatemodel.cpp                 | 29 ++++++++++++
72ccf7
 5 files changed, 88 insertions(+), 1 deletion(-)
72ccf7
 create mode 100644 tests/auto/qml/qqmldelegatemodel/data/redrawUponColumnChange.qml
72ccf7
72ccf7
diff --git a/src/qmlmodels/qqmldelegatemodel.cpp b/src/qmlmodels/qqmldelegatemodel.cpp
72e842
index e6aff955fe..381726291d 100644
72ccf7
--- a/src/qmlmodels/qqmldelegatemodel.cpp
72ccf7
+++ b/src/qmlmodels/qqmldelegatemodel.cpp
72ccf7
@@ -389,6 +389,12 @@ void QQmlDelegateModelPrivate::connectToAbstractItemModel()
72ccf7
                       q,  QQmlDelegateModel, SLOT(_q_rowsRemoved(QModelIndex,int,int)));
72ccf7
     qmlobject_connect(aim, QAbstractItemModel, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
72ccf7
                       q,  QQmlDelegateModel, SLOT(_q_rowsAboutToBeRemoved(QModelIndex,int,int)));
72ccf7
+    qmlobject_connect(aim, QAbstractItemModel, SIGNAL(columnsInserted(QModelIndex,int,int)),
72ccf7
+                      q, QQmlDelegateModel, SLOT(_q_columnsInserted(QModelIndex,int,int)));
72ccf7
+    qmlobject_connect(aim, QAbstractItemModel, SIGNAL(columnsRemoved(QModelIndex,int,int)),
72ccf7
+                      q, QQmlDelegateModel, SLOT(_q_columnsRemoved(QModelIndex,int,int)));
72ccf7
+    qmlobject_connect(aim, QAbstractItemModel, SIGNAL(columnsMoved(QModelIndex,int,int,QModelIndex,int)),
72ccf7
+                      q, QQmlDelegateModel, SLOT(_q_columnsMoved(QModelIndex,int,int,QModelIndex,int)));
72ccf7
     qmlobject_connect(aim, QAbstractItemModel, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)),
72ccf7
                       q, QQmlDelegateModel, SLOT(_q_dataChanged(QModelIndex,QModelIndex,QVector<int>)));
72ccf7
     qmlobject_connect(aim, QAbstractItemModel, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)),
72ccf7
@@ -413,6 +419,12 @@ void QQmlDelegateModelPrivate::disconnectFromAbstractItemModel()
72ccf7
                         q, SLOT(_q_rowsAboutToBeRemoved(QModelIndex,int,int)));
72ccf7
     QObject::disconnect(aim, SIGNAL(rowsRemoved(QModelIndex,int,int)),
72ccf7
                         q, SLOT(_q_rowsRemoved(QModelIndex,int,int)));
72ccf7
+    QObject::disconnect(aim, SIGNAL(columnsInserted(QModelIndex,int,int)), q,
72ccf7
+                        SLOT(_q_columnsInserted(QModelIndex,int,int)));
72ccf7
+    QObject::disconnect(aim, SIGNAL(columnsRemoved(QModelIndex,int,int)), q,
72ccf7
+                        SLOT(_q_columnsRemoved(QModelIndex,int,int)));
72ccf7
+    QObject::disconnect(aim, SIGNAL(columnsMoved(QModelIndex,int,int,QModelIndex,int)), q,
72ccf7
+                        SLOT(_q_columnsMoved(QModelIndex,int,int,QModelIndex,int)));
72ccf7
     QObject::disconnect(aim, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)),
72ccf7
                         q, SLOT(_q_dataChanged(QModelIndex,QModelIndex,QVector<int>)));
72ccf7
     QObject::disconnect(aim, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)),
72e842
@@ -1958,6 +1970,38 @@ void QQmlDelegateModel::_q_rowsMoved(
72ccf7
     }
72ccf7
 }
72ccf7
 
72ccf7
+void QQmlDelegateModel::_q_columnsInserted(const QModelIndex &parent, int begin, int end)
72ccf7
+{
72ccf7
+    Q_D(QQmlDelegateModel);
72ccf7
+    Q_UNUSED(end);
72ccf7
+    if (parent == d->m_adaptorModel.rootIndex && begin == 0) {
72ccf7
+        // mark all items as changed
72ccf7
+        _q_itemsChanged(0, d->m_count, QVector<int>());
72ccf7
+    }
72ccf7
+}
72ccf7
+
72ccf7
+void QQmlDelegateModel::_q_columnsRemoved(const QModelIndex &parent, int begin, int end)
72ccf7
+{
72ccf7
+    Q_D(QQmlDelegateModel);
72ccf7
+    Q_UNUSED(end);
72ccf7
+    if (parent == d->m_adaptorModel.rootIndex && begin == 0) {
72ccf7
+        // mark all items as changed
72ccf7
+        _q_itemsChanged(0, d->m_count, QVector<int>());
72ccf7
+    }
72ccf7
+}
72ccf7
+
72ccf7
+void QQmlDelegateModel::_q_columnsMoved(const QModelIndex &parent, int start, int end,
72ccf7
+                                        const QModelIndex &destination, int column)
72ccf7
+{
72ccf7
+    Q_D(QQmlDelegateModel);
72ccf7
+    Q_UNUSED(end);
72ccf7
+    if ((parent == d->m_adaptorModel.rootIndex && start == 0)
72ccf7
+        || (destination == d->m_adaptorModel.rootIndex && column == 0)) {
72ccf7
+        // mark all items as changed
72ccf7
+        _q_itemsChanged(0, d->m_count, QVector<int>());
72ccf7
+    }
72ccf7
+}
72ccf7
+
72ccf7
 void QQmlDelegateModel::_q_dataChanged(const QModelIndex &begin, const QModelIndex &end, const QVector<int> &roles)
72ccf7
 {
72ccf7
     Q_D(QQmlDelegateModel);
72ccf7
diff --git a/src/qmlmodels/qqmldelegatemodel_p.h b/src/qmlmodels/qqmldelegatemodel_p.h
72ccf7
index 8aab4badca..d140bfbaaf 100644
72ccf7
--- a/src/qmlmodels/qqmldelegatemodel_p.h
72ccf7
+++ b/src/qmlmodels/qqmldelegatemodel_p.h
72ccf7
@@ -152,6 +152,9 @@ private Q_SLOTS:
72ccf7
     void _q_itemsMoved(int from, int to, int count);
72ccf7
     void _q_modelReset();
72ccf7
     void _q_rowsInserted(const QModelIndex &,int,int);
72ccf7
+    void _q_columnsInserted(const QModelIndex &, int, int);
72ccf7
+    void _q_columnsRemoved(const QModelIndex &, int, int);
72ccf7
+    void _q_columnsMoved(const QModelIndex &, int, int, const QModelIndex &, int);
72ccf7
     void _q_rowsAboutToBeRemoved(const QModelIndex &parent, int begin, int end);
72ccf7
     void _q_rowsRemoved(const QModelIndex &,int,int);
72ccf7
     void _q_rowsMoved(const QModelIndex &, int, int, const QModelIndex &, int);
72ccf7
diff --git a/tests/auto/qml/qqmldelegatemodel/data/redrawUponColumnChange.qml b/tests/auto/qml/qqmldelegatemodel/data/redrawUponColumnChange.qml
72ccf7
new file mode 100644
72ccf7
index 0000000000..206133bb39
72ccf7
--- /dev/null
72ccf7
+++ b/tests/auto/qml/qqmldelegatemodel/data/redrawUponColumnChange.qml
72ccf7
@@ -0,0 +1,11 @@
72ccf7
+import QtQuick 2.8
72ccf7
+
72ccf7
+ListView {
72ccf7
+    id: root
72ccf7
+    width: 200
72ccf7
+    height: 200
72ccf7
+
72ccf7
+    delegate: Text {
72ccf7
+        text: display
72ccf7
+    }
72ccf7
+}
72ccf7
diff --git a/tests/auto/qml/qqmldelegatemodel/qqmldelegatemodel.pro b/tests/auto/qml/qqmldelegatemodel/qqmldelegatemodel.pro
72ccf7
index 7fdd3ab5f1..fbd72f6a44 100644
72ccf7
--- a/tests/auto/qml/qqmldelegatemodel/qqmldelegatemodel.pro
72ccf7
+++ b/tests/auto/qml/qqmldelegatemodel/qqmldelegatemodel.pro
72ccf7
@@ -2,7 +2,7 @@ CONFIG += testcase
72ccf7
 TARGET = tst_qqmldelegatemodel
72ccf7
 macos:CONFIG -= app_bundle
72ccf7
 
72ccf7
-QT += qml testlib core-private qml-private qmlmodels-private
72ccf7
+QT += qml quick testlib core-private qml-private qmlmodels-private
72ccf7
 
72ccf7
 SOURCES += tst_qqmldelegatemodel.cpp
72ccf7
 
72ccf7
diff --git a/tests/auto/qml/qqmldelegatemodel/tst_qqmldelegatemodel.cpp b/tests/auto/qml/qqmldelegatemodel/tst_qqmldelegatemodel.cpp
72ccf7
index 87f42c0c8a..1d338ac330 100644
72ccf7
--- a/tests/auto/qml/qqmldelegatemodel/tst_qqmldelegatemodel.cpp
72ccf7
+++ b/tests/auto/qml/qqmldelegatemodel/tst_qqmldelegatemodel.cpp
72ccf7
@@ -27,8 +27,12 @@
72ccf7
 ****************************************************************************/
72ccf7
 
72ccf7
 #include <QtTest/qtest.h>
72ccf7
+#include <QtCore/QConcatenateTablesProxyModel>
72ccf7
+#include <QtGui/QStandardItemModel>
72ccf7
 #include <QtQml/qqmlcomponent.h>
72ccf7
 #include <QtQmlModels/private/qqmldelegatemodel_p.h>
72ccf7
+#include <QtQuick/qquickview.h>
72ccf7
+#include <QtQuick/qquickitem.h>
72ccf7
 
72ccf7
 #include "../../shared/util.h"
72ccf7
 
72ccf7
@@ -42,6 +46,7 @@ public:
72ccf7
 private slots:
72ccf7
     void valueWithoutCallingObjectFirst_data();
72ccf7
     void valueWithoutCallingObjectFirst();
72ccf7
+    void redrawUponColumnChange();
72ccf7
 };
72ccf7
 
72ccf7
 class AbstractItemModel : public QAbstractItemModel
72ccf7
@@ -134,6 +139,30 @@ void tst_QQmlDelegateModel::valueWithoutCallingObjectFirst()
72ccf7
     QCOMPARE(model->variantValue(index, role), expectedValue);
72ccf7
 }
72ccf7
 
72ccf7
+void tst_QQmlDelegateModel::redrawUponColumnChange()
72ccf7
+{
72ccf7
+    QStandardItemModel m1;
72ccf7
+    m1.appendRow({
72ccf7
+            new QStandardItem("Banana"),
72ccf7
+            new QStandardItem("Coconut"),
72ccf7
+    });
72ccf7
+
72ccf7
+    QQuickView view(testFileUrl("redrawUponColumnChange.qml"));
72ccf7
+    QCOMPARE(view.status(), QQuickView::Ready);
72ccf7
+    view.show();
72ccf7
+    QQuickItem *root = view.rootObject();
72ccf7
+    root->setProperty("model", QVariant::fromValue<QObject *>(&m1));
72ccf7
+
72ccf7
+    QObject *item = root->property("currentItem").value<QObject *>();
72ccf7
+    QVERIFY(item);
72ccf7
+    QCOMPARE(item->property("text").toString(), "Banana");
72ccf7
+
72ccf7
+    QVERIFY(root);
72ccf7
+    m1.removeColumn(0);
72ccf7
+
72ccf7
+    QCOMPARE(item->property("text").toString(), "Coconut");
72ccf7
+}
72ccf7
+
72ccf7
 QTEST_MAIN(tst_QQmlDelegateModel)
72ccf7
 
72ccf7
 #include "tst_qqmldelegatemodel.moc"
72ccf7
-- 
72e842
2.35.1
72ccf7