diff options
author | Antti Määttä <antti.maatta@qt.io> | 2024-04-12 09:03:05 +0300 |
---|---|---|
committer | Antti Määttä <antti.maatta@qt.io> | 2024-04-17 08:14:46 +0300 |
commit | 4f1c1ba238e382697cee636bb544e92532d9ba51 (patch) | |
tree | 24346b6bb8cd1e1fc67134ca71641d9a9dcdb013 /tests | |
parent | 2e1400f8b4296e74c65e6c4c56be6aa20b0733b5 (diff) |
Fix adding columns to TreeView
Handle the column related signals in the qqmltreemodeltotablemodel.
Fixes: QTBUG-94100
Pic-to: 6.7 6.6 6.5
Change-Id: I4a1d4ee4e2f927e7798211f314a1356721748c92
Reviewed-by: Antti Määttä <antti.maatta@qt.io>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/qml/qqmltreemodeltotablemodel/testmodel.cpp | 28 | ||||
-rw-r--r-- | tests/auto/qml/qqmltreemodeltotablemodel/testmodel.h | 1 | ||||
-rw-r--r-- | tests/auto/quick/qquicktreeview/testmodel.cpp | 30 | ||||
-rw-r--r-- | tests/auto/quick/qquicktreeview/testmodel.h | 1 | ||||
-rw-r--r-- | tests/auto/quick/qquicktreeview/tst_qquicktreeview.cpp | 43 |
5 files changed, 103 insertions, 0 deletions
diff --git a/tests/auto/qml/qqmltreemodeltotablemodel/testmodel.cpp b/tests/auto/qml/qqmltreemodeltotablemodel/testmodel.cpp index e0e055c28d..7cf077f265 100644 --- a/tests/auto/qml/qqmltreemodeltotablemodel/testmodel.cpp +++ b/tests/auto/qml/qqmltreemodeltotablemodel/testmodel.cpp @@ -129,3 +129,31 @@ bool TestModel::insertRows(int position, int rows, const QModelIndex &parent) endInsertRows(); return true; } + +void insertColumnsRecursive(TreeItem *item, int cols) +{ + int pos = item->m_entries.size(); + for (int col = 0; col < cols; col++) + item->m_entries << QVariant(QString("%1, %2 (inserted)").arg(pos + col).arg(col)); + for (auto child : item->m_childItems) + insertColumnsRecursive(child, cols); +} + +bool TestModel::insertColumns(int position, int cols, const QModelIndex &parent) +{ + if (!parent.isValid()) { + qWarning() << "Cannot insert columns on an invalid parent!"; + return false; + } + + beginInsertColumns(parent, position, position + cols - 1); + TreeItem *parentItem = treeItem(parent); + + TreeItem *item = m_rootItem.data(); + + insertColumnsRecursive(item, cols); + m_columnCount += cols; + + endInsertColumns(); + return true; +} diff --git a/tests/auto/qml/qqmltreemodeltotablemodel/testmodel.h b/tests/auto/qml/qqmltreemodeltotablemodel/testmodel.h index e0c4a9953e..c67410d5f1 100644 --- a/tests/auto/qml/qqmltreemodeltotablemodel/testmodel.h +++ b/tests/auto/qml/qqmltreemodeltotablemodel/testmodel.h @@ -38,6 +38,7 @@ public: QModelIndex parent(const QModelIndex &index) const override; bool insertRows(int position, int rows, const QModelIndex &parent) override; + bool insertColumns(int position, int rows, const QModelIndex &parent) override; private: QScopedPointer<TreeItem> m_rootItem; diff --git a/tests/auto/quick/qquicktreeview/testmodel.cpp b/tests/auto/quick/qquicktreeview/testmodel.cpp index 85e763e151..66fbf9b656 100644 --- a/tests/auto/quick/qquicktreeview/testmodel.cpp +++ b/tests/auto/quick/qquicktreeview/testmodel.cpp @@ -128,3 +128,33 @@ bool TestModel::insertRows(int position, int rows, const QModelIndex &parent) endInsertRows(); return true; } + + +void insertColumnsRecursive(TreeItem *item, int row, int pos, int cols) +{ + for (int col = 0; col < cols; col++) + item->m_entries.insert(pos + col, QVariant(QString("%1, %2 (inserted)").arg(row).arg(pos + col))); + for (auto child : item->m_childItems) { + insertColumnsRecursive(child, row, pos, cols); + row++; + } +} + +bool TestModel::insertColumns(int position, int cols, const QModelIndex &parent) +{ + if (!parent.isValid()) { + qWarning() << "Cannot insert columns on an invalid parent!"; + return false; + } + + beginInsertColumns(parent, position, position + cols - 1); + TreeItem *parentItem = treeItem(parent); + + TreeItem *item = m_rootItem.data(); + + insertColumnsRecursive(item, 0, position, cols); + m_columnCount += cols; + + endInsertColumns(); + return true; +} diff --git a/tests/auto/quick/qquicktreeview/testmodel.h b/tests/auto/quick/qquicktreeview/testmodel.h index cf2213d08b..e12d0ae1d6 100644 --- a/tests/auto/quick/qquicktreeview/testmodel.h +++ b/tests/auto/quick/qquicktreeview/testmodel.h @@ -39,6 +39,7 @@ public: int maxDepth() { return 4; } bool insertRows(int position, int rows, const QModelIndex &parent) override; + bool insertColumns(int position, int cols, const QModelIndex &parent) override; private: QScopedPointer<TreeItem> m_rootItem; diff --git a/tests/auto/quick/qquicktreeview/tst_qquicktreeview.cpp b/tests/auto/quick/qquicktreeview/tst_qquicktreeview.cpp index 763e1a9c80..d414cf8a1a 100644 --- a/tests/auto/quick/qquicktreeview/tst_qquicktreeview.cpp +++ b/tests/auto/quick/qquicktreeview/tst_qquicktreeview.cpp @@ -76,6 +76,7 @@ private slots: void emptyModel(); void updatedModifiedModel(); void insertRows(); + void insertColumns(); void toggleExpandedUsingArrowKeys(); void expandAndCollapsUsingDoubleClick(); void selectionBehaviorCells_data(); @@ -392,6 +393,48 @@ void tst_qquicktreeview::insertRows() QCOMPARE(treeView->rows(), 9); } +void tst_qquicktreeview::insertColumns() +{ + // Check that if we add new columns to the model, TreeView gets updated + // to contain the new expected number of rows (flattened to a list) + LOAD_TREEVIEW("normaltreeview.qml"); + treeView->expand(0); + WAIT_UNTIL_POLISHED; + + QCOMPARE(treeView->columns(), 5); + + const QModelIndex rootNode = model->index(0, 0, QModelIndex()); + model->insertColumns(0, 2, rootNode); + WAIT_UNTIL_POLISHED; + + QCOMPARE(treeView->columns(), 7); + auto childItem1 = treeViewPrivate->loadedTableItem(QPoint(0, 1))->item; + QCOMPARE(childItem1->property("text").toString(), "0, 0 (inserted)"); + auto childItem2 = treeViewPrivate->loadedTableItem(QPoint(0, 2))->item; + QCOMPARE(childItem2->property("text").toString(), "1, 0 (inserted)"); + auto childItem3 = treeViewPrivate->loadedTableItem(QPoint(0, 3))->item; + QCOMPARE(childItem3->property("text").toString(), "2, 0 (inserted)"); + auto childItem4 = treeViewPrivate->loadedTableItem(QPoint(3, 0))->item; + QCOMPARE(childItem4->property("text").toString(), "0, 1"); + auto childItem5 = treeViewPrivate->loadedTableItem(QPoint(3, 1))->item; + QCOMPARE(childItem5->property("text").toString(), "0, 1"); + + const QModelIndex indexOfInsertedChild = model->index(1, 0, rootNode); + model->insertRows(0, 2, indexOfInsertedChild); + treeView->expand(2); + WAIT_UNTIL_POLISHED; + + QCOMPARE(treeView->rows(), 7); + QCOMPARE(treeView->columns(), 7); + + for (int i = 0; i < 7; i++) { + for (int j = 0; j < 7; j++) { + auto childItem = treeViewPrivate->loadedTableItem(QPoint(j, i))->item; + QVERIFY(childItem); + } + } +} + void tst_qquicktreeview::expandChildPendingToBeVisible() { // Check that if we expand a row r1, and that row has a child r2 that can |