diff options
author | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2018-10-15 14:35:15 +0200 |
---|---|---|
committer | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2018-10-17 10:59:36 +0000 |
commit | d973907f6e4aae492dfad3eaad6827ffdc49962b (patch) | |
tree | c32a0132143f87d703d07dda2e0361ebefb0a282 | |
parent | b09bc0c0ebd8ff35ab5e77b21bd1fdd590347488 (diff) |
TableView: connect to 'layoutChanged' signal from the model
Ensure we rebuild the table when the model emits 'layoutChanged'.
Fixes: QTBUG-71140
Change-Id: I70dac897830bf5a12ae6987920e388743fd358a1
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
-rw-r--r-- | src/quick/items/qquicktableview.cpp | 10 | ||||
-rw-r--r-- | src/quick/items/qquicktableview_p_p.h | 1 | ||||
-rw-r--r-- | tests/auto/quick/qquicktableview/testmodel.h | 14 | ||||
-rw-r--r-- | tests/auto/quick/qquicktableview/tst_qquicktableview.cpp | 39 |
4 files changed, 62 insertions, 2 deletions
diff --git a/src/quick/items/qquicktableview.cpp b/src/quick/items/qquicktableview.cpp index 68d55d98ea..44c16e2d7e 100644 --- a/src/quick/items/qquicktableview.cpp +++ b/src/quick/items/qquicktableview.cpp @@ -1664,6 +1664,7 @@ void QQuickTableViewPrivate::connectToModel() connect(aim, &QAbstractItemModel::columnsInserted, this, &QQuickTableViewPrivate::columnsInsertedCallback); connect(aim, &QAbstractItemModel::columnsRemoved, this, &QQuickTableViewPrivate::columnsRemovedCallback); connect(aim, &QAbstractItemModel::modelReset, this, &QQuickTableViewPrivate::modelResetCallback); + connect(aim, &QAbstractItemModel::layoutChanged, this, &QQuickTableViewPrivate::layoutChangedCallback); } else { QObjectPrivate::connect(model, &QQmlInstanceModel::modelUpdated, this, &QQuickTableViewPrivate::modelUpdated); } @@ -1689,6 +1690,7 @@ void QQuickTableViewPrivate::disconnectFromModel() disconnect(aim, &QAbstractItemModel::columnsInserted, this, &QQuickTableViewPrivate::columnsInsertedCallback); disconnect(aim, &QAbstractItemModel::columnsRemoved, this, &QQuickTableViewPrivate::columnsRemovedCallback); disconnect(aim, &QAbstractItemModel::modelReset, this, &QQuickTableViewPrivate::modelResetCallback); + disconnect(aim, &QAbstractItemModel::layoutChanged, this, &QQuickTableViewPrivate::layoutChangedCallback); } else { QObjectPrivate::disconnect(model, &QQmlInstanceModel::modelUpdated, this, &QQuickTableViewPrivate::modelUpdated); } @@ -1751,6 +1753,14 @@ void QQuickTableViewPrivate::columnsRemovedCallback(const QModelIndex &parent, i scheduleRebuildTable(RebuildOption::ViewportOnly); } +void QQuickTableViewPrivate::layoutChangedCallback(const QList<QPersistentModelIndex> &parents, QAbstractItemModel::LayoutChangeHint hint) +{ + Q_UNUSED(parents); + Q_UNUSED(hint); + + scheduleRebuildTable(RebuildOption::ViewportOnly); +} + void QQuickTableViewPrivate::modelResetCallback() { scheduleRebuildTable(RebuildOption::All); diff --git a/src/quick/items/qquicktableview_p_p.h b/src/quick/items/qquicktableview_p_p.h index a4f829addd..2ed04f8d29 100644 --- a/src/quick/items/qquicktableview_p_p.h +++ b/src/quick/items/qquicktableview_p_p.h @@ -356,6 +356,7 @@ public: void rowsRemovedCallback(const QModelIndex &parent, int begin, int end); void columnsInsertedCallback(const QModelIndex &parent, int begin, int end); void columnsRemovedCallback(const QModelIndex &parent, int begin, int end); + void layoutChangedCallback(const QList<QPersistentModelIndex> &parents, QAbstractItemModel::LayoutChangeHint hint); void modelResetCallback(); void _q_componentFinalized(); diff --git a/tests/auto/quick/qquicktableview/testmodel.h b/tests/auto/quick/qquicktableview/testmodel.h index b1bb7a41b8..50f434019e 100644 --- a/tests/auto/quick/qquicktableview/testmodel.h +++ b/tests/auto/quick/qquicktableview/testmodel.h @@ -68,14 +68,13 @@ public: return { {Qt::DisplayRole, "display"} }; } - Q_INVOKABLE void setModelData(const QPoint &cell, const QSize &span, const QString &prefix) + Q_INVOKABLE void setModelData(const QPoint &cell, const QSize &span, const QString &string) { for (int c = 0; c < span.width(); ++c) { for (int r = 0; r < span.height(); ++r) { const int changedRow = cell.y() + r; const int changedColumn = cell.x() + c; const int serializedIndex = changedRow + (changedColumn * m_rows); - const QString string = prefix + QStringLiteral("%1,%2").arg(changedColumn).arg(changedRow); modelData.insert(serializedIndex, string); } } @@ -129,6 +128,17 @@ public: return true; } + void swapRows(int row1, int row2) + { + layoutAboutToBeChanged(); + Q_ASSERT(modelData.contains(row1)); + Q_ASSERT(modelData.contains(row2)); + const QString tmp = modelData[row1]; + modelData[row1] = modelData[row2]; + modelData[row2] = tmp; + layoutChanged(); + } + void clear() { beginResetModel(); m_rows = 0; diff --git a/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp b/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp index 38789fd7bf..d475ef9c4d 100644 --- a/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp +++ b/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp @@ -141,6 +141,7 @@ private slots: void flickOvershoot(); void checkRowColumnCount(); void modelSignals(); + void checkModelSignalsUpdateLayout(); void dataChangedSignal(); void checkThatPoolIsDrainedWhenReuseIsFalse(); void checkIfDelegatesAreReused_data(); @@ -1472,6 +1473,44 @@ void tst_QQuickTableView::modelSignals() QCOMPARE(tableView->columns(), 1); } +void tst_QQuickTableView::checkModelSignalsUpdateLayout() +{ + // Check that if the model rearranges rows and emit the + // 'layoutChanged' signal, TableView will be updated correctly. + LOAD_TABLEVIEW("plaintableview.qml"); + + TestModel model(0, 1); + tableView->setModel(QVariant::fromValue(&model)); + WAIT_UNTIL_POLISHED; + + QCOMPARE(tableView->rows(), 0); + QCOMPARE(tableView->columns(), 1); + + QString modelRow1Text = QStringLiteral("firstRow"); + QString modelRow2Text = QStringLiteral("secondRow"); + model.insertRow(0); + model.insertRow(0); + model.setModelData(QPoint(0, 0), QSize(1, 1), modelRow1Text); + model.setModelData(QPoint(0, 1), QSize(1, 1), modelRow2Text); + WAIT_UNTIL_POLISHED; + + QCOMPARE(tableView->rows(), 2); + QCOMPARE(tableView->columns(), 1); + + QString delegate1text = tableViewPrivate->loadedTableItem(QPoint(0, 0))->item->property("modelDataBinding").toString(); + QString delegate2text = tableViewPrivate->loadedTableItem(QPoint(0, 1))->item->property("modelDataBinding").toString(); + QCOMPARE(delegate1text, modelRow1Text); + QCOMPARE(delegate2text, modelRow2Text); + + model.swapRows(0, 1); + WAIT_UNTIL_POLISHED; + + delegate1text = tableViewPrivate->loadedTableItem(QPoint(0, 0))->item->property("modelDataBinding").toString(); + delegate2text = tableViewPrivate->loadedTableItem(QPoint(0, 1))->item->property("modelDataBinding").toString(); + QCOMPARE(delegate1text, modelRow2Text); + QCOMPARE(delegate2text, modelRow1Text); +} + void tst_QQuickTableView::dataChangedSignal() { // Check that bindings to the model inside a delegate gets updated |