summaryrefslogtreecommitdiffstats
path: root/tests/auto/gui/itemmodels
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/gui/itemmodels')
-rw-r--r--tests/auto/gui/itemmodels/CMakeLists.txt3
-rw-r--r--tests/auto/gui/itemmodels/qfilesystemmodel/CMakeLists.txt20
-rw-r--r--tests/auto/gui/itemmodels/qfilesystemmodel/tst_qfilesystemmodel.cpp138
-rw-r--r--tests/auto/gui/itemmodels/qstandarditem/CMakeLists.txt11
-rw-r--r--tests/auto/gui/itemmodels/qstandarditem/tst_qstandarditem.cpp72
-rw-r--r--tests/auto/gui/itemmodels/qstandarditemmodel/CMakeLists.txt12
-rw-r--r--tests/auto/gui/itemmodels/qstandarditemmodel/tst_qstandarditemmodel.cpp241
7 files changed, 343 insertions, 154 deletions
diff --git a/tests/auto/gui/itemmodels/CMakeLists.txt b/tests/auto/gui/itemmodels/CMakeLists.txt
index 69b6cb0e22..4c25418ef1 100644
--- a/tests/auto/gui/itemmodels/CMakeLists.txt
+++ b/tests/auto/gui/itemmodels/CMakeLists.txt
@@ -1,4 +1,5 @@
-# Generated from itemmodels.pro.
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
add_subdirectory(qstandarditem)
if(TARGET Qt::Widgets)
diff --git a/tests/auto/gui/itemmodels/qfilesystemmodel/CMakeLists.txt b/tests/auto/gui/itemmodels/qfilesystemmodel/CMakeLists.txt
index 31fd8ca32d..85fb4fe2e1 100644
--- a/tests/auto/gui/itemmodels/qfilesystemmodel/CMakeLists.txt
+++ b/tests/auto/gui/itemmodels/qfilesystemmodel/CMakeLists.txt
@@ -1,25 +1,23 @@
-# Generated from qfilesystemmodel.pro.
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_qfilesystemmodel Test:
#####################################################################
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qfilesystemmodel LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
qt_internal_add_test(tst_qfilesystemmodel
SOURCES
tst_qfilesystemmodel.cpp
- PUBLIC_LIBRARIES
+ LIBRARIES
Qt::CorePrivate
Qt::Gui
Qt::Widgets
Qt::WidgetsPrivate
Qt::TestPrivate
)
-
-## Scopes:
-#####################################################################
-
-#### Keys ignored in scope 2:.:.:qfilesystemmodel.pro:WIN32:
-# testcase.timeout = "900"
-
-#### Keys ignored in scope 3:.:.:qfilesystemmodel.pro:MACOS:
-# testcase.timeout = "900"
diff --git a/tests/auto/gui/itemmodels/qfilesystemmodel/tst_qfilesystemmodel.cpp b/tests/auto/gui/itemmodels/qfilesystemmodel/tst_qfilesystemmodel.cpp
index ec724c284f..8ef0b6272a 100644
--- a/tests/auto/gui/itemmodels/qfilesystemmodel/tst_qfilesystemmodel.cpp
+++ b/tests/auto/gui/itemmodels/qfilesystemmodel/tst_qfilesystemmodel.cpp
@@ -1,5 +1,5 @@
// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QTest>
@@ -29,6 +29,7 @@
#include <algorithm>
using namespace Qt::StringLiterals;
+using namespace std::chrono;
#define WAITTIME 1000
@@ -64,6 +65,7 @@ private slots:
void rootPath();
void readOnly();
void iconProvider();
+ void nullIconProvider();
void rowCount();
@@ -79,6 +81,8 @@ private slots:
void filters_data();
void filters();
+ void showFilesOnly();
+
void nameFilters();
void setData_data();
@@ -173,7 +177,7 @@ void tst_QFileSystemModel::rootPath()
QSignalSpy rootChanged(model.data(), &QFileSystemModel::rootPathChanged);
QModelIndex root = model->setRootPath(model->rootPath());
root = model->setRootPath("this directory shouldn't exist");
- QCOMPARE(rootChanged.count(), 0);
+ QCOMPARE(rootChanged.size(), 0);
QString oldRootPath = model->rootPath();
const QStringList documentPaths = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation);
@@ -190,26 +194,26 @@ void tst_QFileSystemModel::rootPath()
QTRY_VERIFY(model->rowCount(root) >= 0);
QCOMPARE(model->rootPath(), QString(documentPath));
- QCOMPARE(rootChanged.count(), oldRootPath == model->rootPath() ? 0 : 1);
+ QCOMPARE(rootChanged.size(), oldRootPath == model->rootPath() ? 0 : 1);
QCOMPARE(model->rootDirectory().absolutePath(), documentPath);
model->setRootPath(QDir::rootPath());
- int oldCount = rootChanged.count();
+ int oldCount = rootChanged.size();
oldRootPath = model->rootPath();
root = model->setRootPath(documentPath + QLatin1String("/."));
QTRY_VERIFY(model->rowCount(root) >= 0);
QCOMPARE(model->rootPath(), documentPath);
- QCOMPARE(rootChanged.count(), oldRootPath == model->rootPath() ? oldCount : oldCount + 1);
+ QCOMPARE(rootChanged.size(), oldRootPath == model->rootPath() ? oldCount : oldCount + 1);
QCOMPARE(model->rootDirectory().absolutePath(), documentPath);
QDir newdir = documentPath;
if (newdir.cdUp()) {
- oldCount = rootChanged.count();
+ oldCount = rootChanged.size();
oldRootPath = model->rootPath();
root = model->setRootPath(documentPath + QLatin1String("/.."));
QTRY_VERIFY(model->rowCount(root) >= 0);
QCOMPARE(model->rootPath(), newdir.path());
- QCOMPARE(rootChanged.count(), oldCount + 1);
+ QCOMPARE(rootChanged.size(), oldCount + 1);
QCOMPARE(model->rootDirectory().absolutePath(), newdir.path());
}
@@ -308,6 +312,19 @@ void tst_QFileSystemModel::iconProvider()
QCOMPARE(myModel->fileIcon(myModel->index(QDir::homePath())).pixmap(50, 50), mb);
}
+void tst_QFileSystemModel::nullIconProvider()
+{
+ QFileSystemModel model;
+ QAbstractItemModelTester tester(&model);
+ tester.setUseFetchMore(false);
+ QVERIFY(model.iconProvider());
+ // No crash when setIconProvider(nullptr) is used
+ model.setIconProvider(nullptr);
+ const auto documentPaths = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation);
+ QVERIFY(!documentPaths.isEmpty());
+ model.setRootPath(documentPaths.constFirst());
+}
+
bool tst_QFileSystemModel::createFiles(QFileSystemModel *model, const QString &test_path,
const QStringList &initial_files, int existingFileCount,
const QStringList &initial_dirs)
@@ -403,8 +420,8 @@ void tst_QFileSystemModel::rowCount()
QModelIndex root = prepareTestModelRoot(model.data(), flatDirTestPath, &spy2, &spy3);
QVERIFY(root.isValid());
- QVERIFY(spy2 && spy2->count() > 0);
- QVERIFY(spy3 && spy3->count() > 0);
+ QVERIFY(spy2 && spy2->size() > 0);
+ QVERIFY(spy3 && spy3->size() > 0);
}
void tst_QFileSystemModel::rowsInserted_data()
@@ -446,7 +463,7 @@ void tst_QFileSystemModel::rowsInserted()
QVERIFY(createFiles(model.data(), tmp, files, 5));
QTRY_COMPARE(model->rowCount(root), oldCount + count);
int totalRowsInserted = 0;
- for (int i = 0; i < spy0.count(); ++i) {
+ for (int i = 0; i < spy0.size(); ++i) {
int start = spy0[i].value(1).toInt();
int end = spy0[i].value(2).toInt();
totalRowsInserted += end - start + 1;
@@ -455,24 +472,24 @@ void tst_QFileSystemModel::rowsInserted()
const QString expected = ascending == Qt::AscendingOrder ? QStringLiteral("j") : QStringLiteral("b");
QTRY_COMPARE(lastEntry(root), expected);
- if (spy0.count() > 0) {
+ if (spy0.size() > 0) {
if (count == 0)
- QCOMPARE(spy0.count(), 0);
+ QCOMPARE(spy0.size(), 0);
else
- QVERIFY(spy0.count() >= 1);
+ QVERIFY(spy0.size() >= 1);
}
- if (count == 0) QCOMPARE(spy1.count(), 0); else QVERIFY(spy1.count() >= 1);
+ if (count == 0) QCOMPARE(spy1.size(), 0); else QVERIFY(spy1.size() >= 1);
QVERIFY(createFiles(model.data(), tmp, QStringList(".hidden_file"), 5 + count));
if (count != 0)
- QTRY_VERIFY(spy0.count() >= 1);
+ QTRY_VERIFY(spy0.size() >= 1);
else
- QTRY_COMPARE(spy0.count(), 0);
+ QTRY_COMPARE(spy0.size(), 0);
if (count != 0)
- QTRY_VERIFY(spy1.count() >= 1);
+ QTRY_VERIFY(spy1.size() >= 1);
else
- QTRY_COMPARE(spy1.count(), 0);
+ QTRY_COMPARE(spy1.size(), 0);
}
void tst_QFileSystemModel::rowsRemoved_data()
@@ -503,14 +520,14 @@ void tst_QFileSystemModel::rowsRemoved()
}
for (int i = 0 ; i < 10; ++i) {
if (count != 0) {
- if (i == 10 || spy0.count() != 0) {
- QVERIFY(spy0.count() >= 1);
- QVERIFY(spy1.count() >= 1);
+ if (i == 10 || spy0.size() != 0) {
+ QVERIFY(spy0.size() >= 1);
+ QVERIFY(spy1.size() >= 1);
}
} else {
- if (i == 10 || spy0.count() == 0) {
- QCOMPARE(spy0.count(), 0);
- QCOMPARE(spy1.count(), 0);
+ if (i == 10 || spy0.size() == 0) {
+ QCOMPARE(spy0.size(), 0);
+ QCOMPARE(spy1.size(), 0);
}
}
QStringList lst;
@@ -529,11 +546,11 @@ void tst_QFileSystemModel::rowsRemoved()
QVERIFY(QFile::remove(tmp + QLatin1String("/.c")));
if (count != 0) {
- QVERIFY(spy0.count() >= 1);
- QVERIFY(spy1.count() >= 1);
+ QVERIFY(spy0.size() >= 1);
+ QVERIFY(spy1.size() >= 1);
} else {
- QCOMPARE(spy0.count(), 0);
- QCOMPARE(spy1.count(), 0);
+ QCOMPARE(spy0.size(), 0);
+ QCOMPARE(spy1.size(), 0);
}
}
@@ -565,7 +582,7 @@ void tst_QFileSystemModel::dataChanged()
QTest::qWait(WAITTIME);
- if (count != 0) QVERIFY(spy.count() >= 1); else QCOMPARE(spy.count(), 0);
+ if (count != 0) QVERIFY(spy.size() >= 1); else QCOMPARE(spy.size(), 0);
}
void tst_QFileSystemModel::filters_data()
@@ -620,7 +637,7 @@ void tst_QFileSystemModel::filters()
QFETCH(QStringList, nameFilters);
QFETCH(int, rowCount);
- if (nameFilters.count() > 0)
+ if (nameFilters.size() > 0)
model->setNameFilters(nameFilters);
model->setNameFilterDisables(false);
model->setFilter(dirFilters);
@@ -632,12 +649,12 @@ void tst_QFileSystemModel::filters()
QDir xFactor(tmp);
QStringList dirEntries;
- if (nameFilters.count() > 0)
+ if (nameFilters.size() > 0)
dirEntries = xFactor.entryList(nameFilters, dirFilters);
else
dirEntries = xFactor.entryList(dirFilters);
- QCOMPARE(dirEntries.count(), rowCount);
+ QCOMPARE(dirEntries.size(), rowCount);
QStringList modelEntries;
@@ -649,7 +666,7 @@ void tst_QFileSystemModel::filters()
QCOMPARE(dirEntries, modelEntries);
#ifdef Q_OS_LINUX
- if (files.count() >= 3 && rowCount >= 3 && rowCount != 5) {
+ if (files.size() >= 3 && rowCount >= 3 && rowCount != 5) {
QString fileName1 = (tmp + '/' + files.at(0));
QString fileName2 = (tmp + '/' + files.at(1));
QString fileName3 = (tmp + '/' + files.at(2));
@@ -675,6 +692,39 @@ void tst_QFileSystemModel::filters()
#endif
}
+void tst_QFileSystemModel::showFilesOnly()
+{
+ QString tmp = flatDirTestPath;
+ QFileSystemModel model;
+ QAbstractItemModelTester tester(&model);
+ tester.setUseFetchMore(false);
+ QVERIFY(createFiles(&model, tmp, QStringList()));
+ const QStringList files{u"a"_s, u"b"_s, u"c"_s};
+ const auto subdir = u"sub_directory"_s;
+ QVERIFY(createFiles(&model, tmp, files, 0, {subdir}));
+
+ // The model changes asynchronously when we run the event loop in the QTRY_...
+ // macros, so the root index returned by an earlier call to setRootPath might
+ // become invalid. Make sure we use a fresh one for each iteration.
+
+ // QTBUG-74471
+ // WHAT: setting the root path of the model to a dir with some files and a subdir
+ QTRY_COMPARE(model.rowCount(model.setRootPath(tmp)), files.size() + 1);
+
+ // Change the model to only show files
+ model.setFilter(QDir::Files);
+ QTRY_COMPARE(model.rowCount(model.setRootPath(tmp)), files.size());
+
+ // WHEN: setting the root path to a subdir
+ QModelIndex subIndex = model.setRootPath(tmp + u'/' + subdir);
+ QTRY_COMPARE(model.rowCount(subIndex), 0);
+
+ // THEN: setting the root path to the previous (parent) dir, the model should
+ // still only show files.
+ // Doubling the default timeout (5s) as this test to fails on macos on the CI
+ QTRY_COMPARE_WITH_TIMEOUT(model.rowCount(model.setRootPath(tmp)), files.size(), 10s);
+}
+
void tst_QFileSystemModel::nameFilters()
{
QStringList list;
@@ -748,7 +798,7 @@ void tst_QFileSystemModel::setData()
tmpIdx = model->index(tmp);
model->fetchMore(tmpIdx);
}
- QTRY_COMPARE(model->rowCount(tmpIdx), files.count());
+ QTRY_COMPARE(model->rowCount(tmpIdx), files.size());
QModelIndex idx = model->index(tmp + '/' + oldFileName);
QCOMPARE(idx.isValid(), true);
@@ -758,16 +808,17 @@ void tst_QFileSystemModel::setData()
QCOMPARE(model->setData(idx, newFileName), success);
model->setReadOnly(true);
if (success) {
- QCOMPARE(spy.count(), 1);
+ QCOMPARE(spy.size(), 1);
QList<QVariant> arguments = spy.takeFirst();
QCOMPARE(model->data(idx, QFileSystemModel::FileNameRole).toString(), newFileName);
+ QCOMPARE(model->data(idx, QFileSystemModel::FileInfoRole).value<QFileInfo>().fileName(), newFileName);
QCOMPARE(model->fileInfo(idx).filePath(), tmp + '/' + newFileName);
QCOMPARE(model->index(arguments.at(0).toString()), model->index(tmp));
QCOMPARE(arguments.at(1).toString(), oldFileName);
QCOMPARE(arguments.at(2).toString(), newFileName);
QCOMPARE(QFile::rename(tmp + '/' + newFileName, tmp + '/' + oldFileName), true);
}
- QTRY_COMPARE(model->rowCount(tmpIdx), files.count());
+ QTRY_COMPARE(model->rowCount(tmpIdx), files.size());
// cleanup
if (!subdirName.isEmpty())
QVERIFY(QDir(tmp).removeRecursively());
@@ -827,13 +878,13 @@ void tst_QFileSystemModel::sort()
//Create a file that will be at the end when sorting by name (For Mac, the default)
//but if we sort by size descending it will be the first
QFile tempFile(dirPath + "/plop2.txt");
- tempFile.open(QIODevice::WriteOnly | QIODevice::Text);
+ QVERIFY(tempFile.open(QIODevice::WriteOnly | QIODevice::Text));
QTextStream out(&tempFile);
out << "The magic number is: " << 49 << "\n";
tempFile.close();
QFile tempFile2(dirPath + "/plop.txt");
- tempFile2.open(QIODevice::WriteOnly | QIODevice::Text);
+ QVERIFY(tempFile2.open(QIODevice::WriteOnly | QIODevice::Text));
QTextStream out2(&tempFile2);
out2 << "The magic number is : " << 49 << " but i write some stuff in the file \n";
tempFile2.close();
@@ -862,7 +913,7 @@ void tst_QFileSystemModel::sort()
expectedOrder << tempFile2.fileName() << tempFile.fileName() << dirPath + QChar('/') + ".." << dirPath + QChar('/') + ".";
if (fileDialogMode) {
- QTRY_COMPARE(myModel->rowCount(parent), expectedOrder.count());
+ QTRY_COMPARE(myModel->rowCount(parent), expectedOrder.size());
// File dialog Mode means sub trees are not sorted, only the current root.
// There's no way we can check that the sub tree is "not sorted"; just check if it
// has the same contents of the expected list
@@ -997,7 +1048,7 @@ void tst_QFileSystemModel::caseSensitivity()
QStringList paths;
QModelIndexList indexes;
QCOMPARE(model->rowCount(root), 0);
- for (int i = 0; i < files.count(); ++i) {
+ for (int i = 0; i < files.size(); ++i) {
const QString path = tmp + '/' + files.at(i);
const QModelIndex index = model->index(path);
QVERIFY(index.isValid());
@@ -1007,7 +1058,7 @@ void tst_QFileSystemModel::caseSensitivity()
if (!QFileSystemEngine::isCaseSensitive()) {
// QTBUG-31103, QTBUG-64147: Verify that files can be accessed by paths with fLipPeD case.
- for (int i = 0; i < paths.count(); ++i) {
+ for (int i = 0; i < paths.size(); ++i) {
const QModelIndex flippedCaseIndex = model->index(flipCase(paths.at(i)));
QCOMPARE(indexes.at(i), flippedCaseIndex);
}
@@ -1028,11 +1079,12 @@ void tst_QFileSystemModel::drives()
QFileSystemModel model;
model.setRootPath(path);
model.fetchMore(QModelIndex());
- QFileInfoList drives = QDir::drives();
+ const QFileInfoList drives = QDir::drives();
int driveCount = 0;
- foreach(const QFileInfo& driveRoot, drives)
+ for (const QFileInfo& driveRoot : drives) {
if (driveRoot.exists())
driveCount++;
+ }
QTRY_COMPARE(model.rowCount(), driveCount);
}
diff --git a/tests/auto/gui/itemmodels/qstandarditem/CMakeLists.txt b/tests/auto/gui/itemmodels/qstandarditem/CMakeLists.txt
index e5446d596a..db29eaaf94 100644
--- a/tests/auto/gui/itemmodels/qstandarditem/CMakeLists.txt
+++ b/tests/auto/gui/itemmodels/qstandarditem/CMakeLists.txt
@@ -1,12 +1,19 @@
-# Generated from qstandarditem.pro.
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_qstandarditem Test:
#####################################################################
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qstandarditem LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
qt_internal_add_test(tst_qstandarditem
SOURCES
tst_qstandarditem.cpp
- PUBLIC_LIBRARIES
+ LIBRARIES
Qt::Gui
)
diff --git a/tests/auto/gui/itemmodels/qstandarditem/tst_qstandarditem.cpp b/tests/auto/gui/itemmodels/qstandarditem/tst_qstandarditem.cpp
index 40834459e5..093367c759 100644
--- a/tests/auto/gui/itemmodels/qstandarditem/tst_qstandarditem.cpp
+++ b/tests/auto/gui/itemmodels/qstandarditem/tst_qstandarditem.cpp
@@ -1,5 +1,5 @@
// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QTest>
@@ -112,13 +112,17 @@ void tst_QStandardItem::getSetData()
item.setToolTip(toolTip);
QCOMPARE(item.toolTip(), toolTip);
+#ifndef QT_NO_STATUSTIP
QString statusTip = QLatin1String("statusTip ") + iS;
item.setStatusTip(statusTip);
QCOMPARE(item.statusTip(), statusTip);
+#endif
+#if QT_CONFIG(whatsthis)
QString whatsThis = QLatin1String("whatsThis ") + iS;
item.setWhatsThis(whatsThis);
QCOMPARE(item.whatsThis(), whatsThis);
+#endif
QSize sizeHint(64*i, 48*i);
item.setSizeHint(sizeHint);
@@ -157,8 +161,12 @@ void tst_QStandardItem::getSetData()
QCOMPARE(item.text(), text);
QCOMPARE(item.icon(), icon);
QCOMPARE(item.toolTip(), toolTip);
+#ifndef QT_NO_STATUSTIP
QCOMPARE(item.statusTip(), statusTip);
+#endif
+#if QT_CONFIG(whatsthis)
QCOMPARE(item.whatsThis(), whatsThis);
+#endif
QCOMPARE(item.sizeHint(), sizeHint);
QCOMPARE(item.font(), font);
QCOMPARE(item.textAlignment(), textAlignment);
@@ -171,8 +179,12 @@ void tst_QStandardItem::getSetData()
QCOMPARE(qvariant_cast<QString>(item.data(Qt::DisplayRole)), text);
QCOMPARE(qvariant_cast<QIcon>(item.data(Qt::DecorationRole)), icon);
QCOMPARE(qvariant_cast<QString>(item.data(Qt::ToolTipRole)), toolTip);
+#ifndef QT_NO_STATUSTIP
QCOMPARE(qvariant_cast<QString>(item.data(Qt::StatusTipRole)), statusTip);
+#endif
+#if QT_CONFIG(whatsthis)
QCOMPARE(qvariant_cast<QString>(item.data(Qt::WhatsThisRole)), whatsThis);
+#endif
QCOMPARE(qvariant_cast<QSize>(item.data(Qt::SizeHintRole)), sizeHint);
QCOMPARE(qvariant_cast<QFont>(item.data(Qt::FontRole)), font);
QCOMPARE(qvariant_cast<int>(item.data(Qt::TextAlignmentRole)), int(textAlignment));
@@ -757,10 +769,10 @@ void tst_QStandardItem::takeColumn()
QList<QStandardItem *> taken = item.takeColumn(column);
if (expectSuccess) {
- QCOMPARE(taken.count(), item.rowCount());
+ QCOMPARE(taken.size(), item.rowCount());
QCOMPARE(item.columnCount(), columns - 1);
int index = column;
- for (int i = 0; i < taken.count(); ++i) {
+ for (int i = 0; i < taken.size(); ++i) {
QCOMPARE(taken.at(i), originalChildren.takeAt(index));
index += item.columnCount();
}
@@ -818,10 +830,10 @@ void tst_QStandardItem::takeRow()
QList<QStandardItem *> taken = item.takeRow(row);
if (expectSuccess) {
- QCOMPARE(taken.count(), item.columnCount());
+ QCOMPARE(taken.size(), item.columnCount());
QCOMPARE(item.rowCount(), rows - 1);
int index = row * columns;
- for (int i = 0; i < taken.count(); ++i) {
+ for (int i = 0; i < taken.size(); ++i) {
QCOMPARE(taken.at(i), originalChildren.takeAt(index));
}
index = 0;
@@ -844,7 +856,9 @@ void tst_QStandardItem::streamItem()
item.setText(QLatin1String("text"));
item.setToolTip(QLatin1String("toolTip"));
item.setStatusTip(QLatin1String("statusTip"));
+#if QT_CONFIG(whatsthis)
item.setWhatsThis(QLatin1String("whatsThis"));
+#endif
item.setSizeHint(QSize(64, 48));
item.setFont(QFont());
item.setTextAlignment(Qt::AlignLeft|Qt::AlignVCenter);
@@ -866,7 +880,9 @@ void tst_QStandardItem::streamItem()
QCOMPARE(streamedItem.text(), item.text());
QCOMPARE(streamedItem.toolTip(), item.toolTip());
QCOMPARE(streamedItem.statusTip(), item.statusTip());
+#if QT_CONFIG(whatsthis)
QCOMPARE(streamedItem.whatsThis(), item.whatsThis());
+#endif
QCOMPARE(streamedItem.sizeHint(), item.sizeHint());
QCOMPARE(streamedItem.font(), item.font());
QCOMPARE(streamedItem.textAlignment(), item.textAlignment());
@@ -905,7 +921,9 @@ void tst_QStandardItem::clone()
item.setText(QLatin1String("text"));
item.setToolTip(QLatin1String("toolTip"));
item.setStatusTip(QLatin1String("statusTip"));
+#if QT_CONFIG(whatsthis)
item.setWhatsThis(QLatin1String("whatsThis"));
+#endif
item.setSizeHint(QSize(64, 48));
item.setFont(QFont());
item.setTextAlignment(Qt::AlignLeft|Qt::AlignVCenter);
@@ -920,7 +938,9 @@ void tst_QStandardItem::clone()
QCOMPARE(clone->text(), item.text());
QCOMPARE(clone->toolTip(), item.toolTip());
QCOMPARE(clone->statusTip(), item.statusTip());
+#if QT_CONFIG(whatsthis)
QCOMPARE(clone->whatsThis(), item.whatsThis());
+#endif
QCOMPARE(clone->sizeHint(), item.sizeHint());
QCOMPARE(clone->font(), item.font());
QCOMPARE(clone->textAlignment(), item.textAlignment());
@@ -984,8 +1004,8 @@ void tst_QStandardItem::sortChildren()
QCOMPARE(two->child(1)->text(), QLatin1String("e"));
QCOMPARE(two->child(2)->text(), QLatin1String("f"));
- QCOMPARE(layoutAboutToBeChangedSpy.count(), (x == 0) ? 0 : 3);
- QCOMPARE(layoutChangedSpy.count(), (x == 0) ? 0 : 3);
+ QCOMPARE(layoutAboutToBeChangedSpy.size(), (x == 0) ? 0 : 3);
+ QCOMPARE(layoutChangedSpy.size(), (x == 0) ? 0 : 3);
if (x == 0)
delete item;
@@ -1001,11 +1021,37 @@ public:
int type() const override { return QStandardItem::UserType + 1; }
bool operator<(const QStandardItem &other) const override {
- return text().length() < other.text().length();
+ return text().size() < other.text().size();
}
using QStandardItem::clone;
using QStandardItem::emitDataChanged;
+
+ void setData(const QVariant &value, int role) override
+ {
+ switch (role) {
+ case Qt::DisplayRole:
+ QStandardItem::setData(value, role);
+ break;
+ default:
+ // setFlags() uses "UserRole - 1" to store the flags, which is an
+ // implementation detail not exposed in the docs.
+ QStandardItem::setData(value, role);
+ break;
+ }
+ }
+
+ QVariant data(int role) const override
+ {
+ switch (role) {
+ case Qt::DisplayRole:
+ return QStandardItem::data(role);
+ default:
+ // flags() uses "UserRole - 1" to get the flags, which is an implementation
+ // detail not exposed in the docs.
+ return QStandardItem::data(role);
+ }
+ }
};
Q_DECLARE_METATYPE(QStandardItem*)
@@ -1027,8 +1073,8 @@ void tst_QStandardItem::subclassing()
QSignalSpy itemChangedSpy(&model, &QStandardItemModel::itemChanged);
item->emitDataChanged();
- QCOMPARE(itemChangedSpy.count(), 1);
- QCOMPARE(itemChangedSpy.at(0).count(), 1);
+ QCOMPARE(itemChangedSpy.size(), 1);
+ QCOMPARE(itemChangedSpy.at(0).size(), 1);
QCOMPARE(qvariant_cast<QStandardItem*>(itemChangedSpy.at(0).at(0)), item);
CustomItem *child0 = new CustomItem("cc");
@@ -1041,6 +1087,12 @@ void tst_QStandardItem::subclassing()
QCOMPARE(item->child(0), child2);
QCOMPARE(item->child(1), child0);
QCOMPARE(item->child(2), child1);
+
+ item->setFlags(Qt::ItemFlags{0});
+ QCOMPARE(item->flags(), Qt::ItemFlags{0});
+
+ item->setFlags(Qt::ItemFlags{Qt::ItemIsEditable | Qt::ItemIsSelectable});
+ QCOMPARE(item->flags(), Qt::ItemFlags{Qt::ItemIsEditable | Qt::ItemIsSelectable});
}
void tst_QStandardItem::lessThan()
diff --git a/tests/auto/gui/itemmodels/qstandarditemmodel/CMakeLists.txt b/tests/auto/gui/itemmodels/qstandarditemmodel/CMakeLists.txt
index 49cbcbfa88..31ae25d008 100644
--- a/tests/auto/gui/itemmodels/qstandarditemmodel/CMakeLists.txt
+++ b/tests/auto/gui/itemmodels/qstandarditemmodel/CMakeLists.txt
@@ -1,16 +1,24 @@
-# Generated from qstandarditemmodel.pro.
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_qstandarditemmodel Test:
#####################################################################
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qstandarditemmodel LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
qt_internal_add_test(tst_qstandarditemmodel
SOURCES
tst_qstandarditemmodel.cpp
- PUBLIC_LIBRARIES
+ LIBRARIES
Qt::CorePrivate
Qt::Gui
Qt::GuiPrivate
Qt::Widgets
Qt::WidgetsPrivate
+ Qt::TestPrivate
)
diff --git a/tests/auto/gui/itemmodels/qstandarditemmodel/tst_qstandarditemmodel.cpp b/tests/auto/gui/itemmodels/qstandarditemmodel/tst_qstandarditemmodel.cpp
index ea9077e2a0..ef2fd83d4c 100644
--- a/tests/auto/gui/itemmodels/qstandarditemmodel/tst_qstandarditemmodel.cpp
+++ b/tests/auto/gui/itemmodels/qstandarditemmodel/tst_qstandarditemmodel.cpp
@@ -1,16 +1,22 @@
// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QTest>
#include <QStandardItemModel>
#include <QTreeView>
+#include <QMap>
#include <QSignalSpy>
#include <QAbstractItemModelTester>
#include <private/qabstractitemmodel_p.h>
+#include <private/qpropertytesthelper_p.h>
#include <private/qtreeview_p.h>
+#include <algorithm>
+
+using namespace Qt::StringLiterals;
+
class tst_QStandardItemModel : public QObject
{
Q_OBJECT
@@ -87,6 +93,7 @@ private slots:
void indexFromItem();
void itemFromIndex();
void getSetItemPrototype();
+ void getSetItemData_data();
void getSetItemData();
void setHeaderLabels_data();
void setHeaderLabels();
@@ -113,6 +120,7 @@ private slots:
void taskQTBUG_45114_setItemData();
void setItemPersistentIndex();
void signalsOnTakeItem();
+ void takeChild();
void createPersistentOnLayoutAboutToBeChanged();
private:
QStandardItemModel *m_model = nullptr;
@@ -448,16 +456,16 @@ void tst_QStandardItemModel::setHeaderData()
for (int i = 0; i < count; ++i) {
QString customString = QString("custom") + QString::number(i);
QCOMPARE(m_model->setHeaderData(i, orient, customString), true);
- QCOMPARE(headerDataChangedSpy.count(), 1);
- QCOMPARE(dataChangedSpy.count(), 0);
+ QCOMPARE(headerDataChangedSpy.size(), 1);
+ QCOMPARE(dataChangedSpy.size(), 0);
QVariantList args = headerDataChangedSpy.takeFirst();
QCOMPARE(qvariant_cast<Qt::Orientation>(args.at(0)), orient);
QCOMPARE(args.at(1).toInt(), i);
QCOMPARE(args.at(2).toInt(), i);
QCOMPARE(m_model->headerData(i, orient).toString(), customString);
QCOMPARE(m_model->setHeaderData(i, orient, customString), true);
- QCOMPARE(headerDataChangedSpy.count(), 0);
- QCOMPARE(dataChangedSpy.count(), 0);
+ QCOMPARE(headerDataChangedSpy.size(), 0);
+ QCOMPARE(dataChangedSpy.size(), 0);
}
//check read from invalid sections
@@ -720,7 +728,7 @@ void tst_QStandardItemModel::data()
const QMap<int, QVariant> itmData = m_model->itemData(m_model->index(0, 0));
QCOMPARE(itmData.value(Qt::DisplayRole), QLatin1String("initialitem"));
QCOMPARE(itmData.value(Qt::ToolTipRole), QLatin1String("tooltip"));
- QVERIFY(!itmData.contains(Qt::UserRole - 1));
+ QVERIFY(!itmData.contains(Qt::UserRole - 1)); // Qt::UserRole - 1 is used to store flags
QVERIFY(m_model->itemData(QModelIndex()).isEmpty());
}
@@ -761,9 +769,9 @@ void tst_QStandardItemModel::clear()
model.clear();
- QCOMPARE(modelResetSpy.count(), 1);
- QCOMPARE(layoutChangedSpy.count(), 0);
- QCOMPARE(rowsRemovedSpy.count(), 0);
+ QCOMPARE(modelResetSpy.size(), 1);
+ QCOMPARE(layoutChangedSpy.size(), 0);
+ QCOMPARE(rowsRemovedSpy.size(), 0);
QCOMPARE(model.index(0, 0), QModelIndex());
QCOMPARE(model.columnCount(), 0);
QCOMPARE(model.rowCount(), 0);
@@ -805,8 +813,8 @@ void tst_QStandardItemModel::sort()
QFETCH(QStringList, expected);
// prepare model
QStandardItemModel model;
- QVERIFY(model.insertRows(0, initial.count(), QModelIndex()));
- QCOMPARE(model.rowCount(QModelIndex()), initial.count());
+ QVERIFY(model.insertRows(0, initial.size(), QModelIndex()));
+ QCOMPARE(model.rowCount(QModelIndex()), initial.size());
model.insertColumns(0, 1, QModelIndex());
QCOMPARE(model.columnCount(QModelIndex()), 1);
for (int row = 0; row < model.rowCount(QModelIndex()); ++row) {
@@ -822,8 +830,8 @@ void tst_QStandardItemModel::sort()
// sort
model.sort(0, sortOrder);
- QCOMPARE(layoutAboutToBeChangedSpy.count(), 1);
- QCOMPARE(layoutChangedSpy.count(), 1);
+ QCOMPARE(layoutAboutToBeChangedSpy.size(), 1);
+ QCOMPARE(layoutChangedSpy.size(), 1);
// make sure the model is sorted
for (int row = 0; row < model.rowCount(QModelIndex()); ++row) {
@@ -867,7 +875,7 @@ void tst_QStandardItemModel::sortRole()
QFETCH(QVariantList, expectedData);
QStandardItemModel model;
- for (int i = 0; i < initialText.count(); ++i) {
+ for (int i = 0; i < initialText.size(); ++i) {
QStandardItem *item = new QStandardItem;
item->setText(initialText.at(i));
item->setData(initialData.at(i), Qt::UserRole);
@@ -875,7 +883,7 @@ void tst_QStandardItemModel::sortRole()
}
model.setSortRole(sortRole);
model.sort(0, sortOrder);
- for (int i = 0; i < expectedText.count(); ++i) {
+ for (int i = 0; i < expectedText.size(); ++i) {
QStandardItem *item = model.item(i);
QCOMPARE(item->text(), expectedText.at(i));
QCOMPARE(item->data(Qt::UserRole), expectedData.at(i));
@@ -896,6 +904,9 @@ void tst_QStandardItemModel::sortRoleBindings()
sortRoleObserver.setBinding([&] { return model.sortRole(); });
model.setSortRole(Qt::EditRole);
QCOMPARE(sortRoleObserver, Qt::EditRole);
+
+ QTestPrivate::testReadWritePropertyBasics(model, static_cast<int>(Qt::DisplayRole),
+ static_cast<int>(Qt::EditRole), "sortRole");
}
void tst_QStandardItemModel::findItems()
@@ -906,15 +917,15 @@ void tst_QStandardItemModel::findItems()
model.item(1)->appendRow(new QStandardItem(QLatin1String("foo")));
QList<QStandardItem*> matches;
matches = model.findItems(QLatin1String("foo"), Qt::MatchExactly|Qt::MatchRecursive, 0);
- QCOMPARE(matches.count(), 2);
+ QCOMPARE(matches.size(), 2);
matches = model.findItems(QLatin1String("foo"), Qt::MatchExactly, 0);
- QCOMPARE(matches.count(), 1);
+ QCOMPARE(matches.size(), 1);
matches = model.findItems(QLatin1String("food"), Qt::MatchExactly|Qt::MatchRecursive, 0);
- QCOMPARE(matches.count(), 0);
+ QCOMPARE(matches.size(), 0);
matches = model.findItems(QLatin1String("foo"), Qt::MatchExactly|Qt::MatchRecursive, -1);
- QCOMPARE(matches.count(), 0);
+ QCOMPARE(matches.size(), 0);
matches = model.findItems(QLatin1String("foo"), Qt::MatchExactly|Qt::MatchRecursive, 1);
- QCOMPARE(matches.count(), 0);
+ QCOMPARE(matches.size(), 0);
}
void tst_QStandardItemModel::getSetHeaderItem()
@@ -1025,33 +1036,49 @@ void tst_QStandardItemModel::getSetItemPrototype()
QCOMPARE(model.itemPrototype(), nullptr);
}
+using RoleMap = QMap<int, QVariant>;
+using RoleList = QList<int>;
+
+static RoleMap getSetItemDataRoleMap(int textRole)
+{
+ return {{textRole, "text"_L1},
+ {Qt::StatusTipRole, "statusTip"_L1},
+ {Qt::ToolTipRole, "toolTip"_L1},
+ {Qt::WhatsThisRole, "whatsThis"_L1},
+ {Qt::SizeHintRole, QSize{64, 48}},
+ {Qt::FontRole, QFont{}},
+ {Qt::TextAlignmentRole, int(Qt::AlignLeft|Qt::AlignVCenter)},
+ {Qt::BackgroundRole, QColor(Qt::blue)},
+ {Qt::ForegroundRole, QColor(Qt::green)},
+ {Qt::CheckStateRole, int(Qt::PartiallyChecked)},
+ {Qt::AccessibleTextRole, "accessibleText"_L1},
+ {Qt::AccessibleDescriptionRole, "accessibleDescription"_L1}};
+}
+
+void tst_QStandardItemModel::getSetItemData_data()
+{
+ QTest::addColumn<RoleMap>("itemData");
+ QTest::addColumn<RoleMap>("expectedItemData");
+ QTest::addColumn<RoleList>("expectedRoles");
+
+ // QTBUG-112326: verify that text data set using Qt::EditRole is mapped to
+ // Qt::DisplayRole and both roles are in the changed signal
+ const RoleMap expectedItemData = getSetItemDataRoleMap(Qt::DisplayRole);
+ RoleList expectedRoles = expectedItemData.keys() << Qt::EditRole;
+ std::sort(expectedRoles.begin(), expectedRoles.end());
+
+ QTest::newRow("DisplayRole") << expectedItemData
+ << expectedItemData << expectedRoles;
+
+ QTest::newRow("EditRole") << getSetItemDataRoleMap(Qt::EditRole)
+ << expectedItemData << expectedRoles;
+}
+
void tst_QStandardItemModel::getSetItemData()
{
- QMap<int, QVariant> roles;
- QLatin1String text("text");
- roles.insert(Qt::DisplayRole, text);
- QLatin1String statusTip("statusTip");
- roles.insert(Qt::StatusTipRole, statusTip);
- QLatin1String toolTip("toolTip");
- roles.insert(Qt::ToolTipRole, toolTip);
- QLatin1String whatsThis("whatsThis");
- roles.insert(Qt::WhatsThisRole, whatsThis);
- QSize sizeHint(64, 48);
- roles.insert(Qt::SizeHintRole, sizeHint);
- QFont font;
- roles.insert(Qt::FontRole, font);
- Qt::Alignment textAlignment(Qt::AlignLeft|Qt::AlignVCenter);
- roles.insert(Qt::TextAlignmentRole, int(textAlignment));
- QColor backgroundColor(Qt::blue);
- roles.insert(Qt::BackgroundRole, backgroundColor);
- QColor textColor(Qt::green);
- roles.insert(Qt::ForegroundRole, textColor);
- Qt::CheckState checkState(Qt::PartiallyChecked);
- roles.insert(Qt::CheckStateRole, int(checkState));
- QLatin1String accessibleText("accessibleText");
- roles.insert(Qt::AccessibleTextRole, accessibleText);
- QLatin1String accessibleDescription("accessibleDescription");
- roles.insert(Qt::AccessibleDescriptionRole, accessibleDescription);
+ QFETCH(RoleMap, itemData);
+ QFETCH(RoleMap, expectedItemData);
+ QFETCH(RoleList, expectedRoles);
QStandardItemModel model;
model.insertRows(0, 1);
@@ -1060,11 +1087,17 @@ void tst_QStandardItemModel::getSetItemData()
QSignalSpy modelDataChangedSpy(
&model, &QStandardItemModel::dataChanged);
- QVERIFY(model.setItemData(idx, roles));
- QCOMPARE(modelDataChangedSpy.count(), 1);
- QVERIFY(model.setItemData(idx, roles));
- QCOMPARE(modelDataChangedSpy.count(), 1); //it was already changed once
- QCOMPARE(model.itemData(idx), roles);
+ QVERIFY(model.setItemData(idx, itemData));
+ QCOMPARE(modelDataChangedSpy.size(), 1);
+ const QVariantList &args = modelDataChangedSpy.constFirst();
+ QCOMPARE(args.size(), 3);
+ auto roleList = args.at(2).value<QList<int> >();
+ std::sort(roleList.begin(), roleList.end());
+ QCOMPARE(roleList, expectedRoles);
+
+ QVERIFY(model.setItemData(idx, itemData));
+ QCOMPARE(modelDataChangedSpy.size(), 1); //it was already changed once
+ QCOMPARE(model.itemData(idx), expectedItemData);
}
void tst_QStandardItemModel::setHeaderLabels_data()
@@ -1127,12 +1160,12 @@ void tst_QStandardItemModel::setHeaderLabels()
model.setHorizontalHeaderLabels(labels);
else
model.setVerticalHeaderLabels(labels);
- for (int i = 0; i < expectedLabels.count(); ++i)
+ for (int i = 0; i < expectedLabels.size(); ++i)
QCOMPARE(model.headerData(i, orientation).toString(), expectedLabels.at(i));
- QCOMPARE(columnsInsertedSpy.count(),
- (orientation == Qt::Vertical) ? 0 : labels.count() > columns);
- QCOMPARE(rowsInsertedSpy.count(),
- (orientation == Qt::Horizontal) ? 0 : labels.count() > rows);
+ QCOMPARE(columnsInsertedSpy.size(),
+ (orientation == Qt::Vertical) ? 0 : labels.size() > columns);
+ QCOMPARE(rowsInsertedSpy.size(),
+ (orientation == Qt::Horizontal) ? 0 : labels.size() > rows);
}
void tst_QStandardItemModel::itemDataChanged()
@@ -1143,8 +1176,8 @@ void tst_QStandardItemModel::itemDataChanged()
QSignalSpy itemChangedSpy(&model, &QStandardItemModel::itemChanged);
model.setItem(0, &item);
- QCOMPARE(dataChangedSpy.count(), 1);
- QCOMPARE(itemChangedSpy.count(), 1);
+ QCOMPARE(dataChangedSpy.size(), 1);
+ QCOMPARE(itemChangedSpy.size(), 1);
QModelIndex index = model.indexFromItem(&item);
QList<QVariant> args;
args = dataChangedSpy.takeFirst();
@@ -1154,8 +1187,8 @@ void tst_QStandardItemModel::itemDataChanged()
QCOMPARE(qvariant_cast<QStandardItem*>(args.at(0)), &item);
item.setData(QLatin1String("foo"), Qt::DisplayRole);
- QCOMPARE(dataChangedSpy.count(), 1);
- QCOMPARE(itemChangedSpy.count(), 1);
+ QCOMPARE(dataChangedSpy.size(), 1);
+ QCOMPARE(itemChangedSpy.size(), 1);
args = dataChangedSpy.takeFirst();
QCOMPARE(qvariant_cast<QModelIndex>(args.at(0)), index);
QCOMPARE(qvariant_cast<QModelIndex>(args.at(1)), index);
@@ -1163,12 +1196,12 @@ void tst_QStandardItemModel::itemDataChanged()
QCOMPARE(qvariant_cast<QStandardItem*>(args.at(0)), &item);
item.setData(item.data(Qt::DisplayRole), Qt::DisplayRole);
- QCOMPARE(dataChangedSpy.count(), 0);
- QCOMPARE(itemChangedSpy.count(), 0);
+ QCOMPARE(dataChangedSpy.size(), 0);
+ QCOMPARE(itemChangedSpy.size(), 0);
item.setFlags(Qt::ItemIsEnabled);
- QCOMPARE(dataChangedSpy.count(), 1);
- QCOMPARE(itemChangedSpy.count(), 1);
+ QCOMPARE(dataChangedSpy.size(), 1);
+ QCOMPARE(itemChangedSpy.size(), 1);
args = dataChangedSpy.takeFirst();
QCOMPARE(qvariant_cast<QModelIndex>(args.at(0)), index);
QCOMPARE(qvariant_cast<QModelIndex>(args.at(1)), index);
@@ -1176,8 +1209,8 @@ void tst_QStandardItemModel::itemDataChanged()
QCOMPARE(qvariant_cast<QStandardItem*>(args.at(0)), &item);
item.setFlags(item.flags());
- QCOMPARE(dataChangedSpy.count(), 0);
- QCOMPARE(itemChangedSpy.count(), 0);
+ QCOMPARE(dataChangedSpy.size(), 0);
+ QCOMPARE(itemChangedSpy.size(), 0);
}
void tst_QStandardItemModel::takeHeaderItem()
@@ -1305,7 +1338,7 @@ void tst_QStandardItemModel::setNullChild()
QSignalSpy spy(&model, &QAbstractItemModel::dataChanged);
item->setChild(0, nullptr);
QCOMPARE(item->child(0), nullptr);
- QCOMPARE(spy.count(), 1);
+ QCOMPARE(spy.size(), 1);
}
void tst_QStandardItemModel::deleteChild()
@@ -1317,7 +1350,7 @@ void tst_QStandardItemModel::deleteChild()
QSignalSpy spy(&model, &QAbstractItemModel::dataChanged);
delete item->child(0);
QCOMPARE(item->child(0), nullptr);
- QCOMPARE(spy.count(), 1);
+ QCOMPARE(spy.size(), 1);
}
void tst_QStandardItemModel::rootItemFlags()
@@ -1558,8 +1591,8 @@ void tst_QStandardItemModel::removeRowsAndColumns()
QStringList row_list = QString("1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20").split(',');
QStringList col_list = row_list;
QStandardItemModel model;
- for (int c = 0; c < col_list.count(); c++)
- for (int r = 0; r < row_list.count(); r++)
+ for (int c = 0; c < col_list.size(); c++)
+ for (int r = 0; r < row_list.size(); r++)
model.setItem(r, c, new QStandardItem(row_list[r] + QLatin1Char('x') + col_list[c]));
VERIFY_MODEL
@@ -1580,16 +1613,22 @@ void tst_QStandardItemModel::removeRowsAndColumns()
VERIFY_MODEL
QList<QStandardItem *> row_taken = model.takeRow(6);
- QCOMPARE(row_taken.count(), col_list.count());
- for (int c = 0; c < col_list.count(); c++)
- QCOMPARE(row_taken[c]->text() , row_list[6] + QLatin1Char('x') + col_list[c]);
+ QCOMPARE(row_taken.size(), col_list.size());
+ for (qsizetype c = 0; c < row_taken.size(); c++) {
+ auto item = row_taken.at(c);
+ QCOMPARE(item->text() , row_list[6] + QLatin1Char('x') + col_list[c]);
+ delete item;
+ }
row_list.remove(6);
VERIFY_MODEL
QList<QStandardItem *> col_taken = model.takeColumn(10);
- QCOMPARE(col_taken.count(), row_list.count());
- for (int r = 0; r < row_list.count(); r++)
- QCOMPARE(col_taken[r]->text() , row_list[r] + QLatin1Char('x') + col_list[10]);
+ QCOMPARE(col_taken.size(), row_list.size());
+ for (qsizetype r = 0; r < col_taken.size(); r++) {
+ auto item = col_taken.at(r);
+ QCOMPARE(item->text() , row_list[r] + QLatin1Char('x') + col_list[10]);
+ delete item;
+ }
col_list.remove(10);
VERIFY_MODEL
}
@@ -1605,8 +1644,8 @@ void tst_QStandardItemModel::itemRoleNames()
QStringList row_list = QString("1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20").split(',');
QStringList col_list = row_list;
QStandardItemModel model;
- for (int c = 0; c < col_list.count(); c++)
- for (int r = 0; r < row_list.count(); r++)
+ for (int c = 0; c < col_list.size(); c++)
+ for (int r = 0; r < row_list.size(); r++)
model.setItem(r, c, new QStandardItem(row_list[r] + QLatin1Char('x') + col_list[c]));
VERIFY_MODEL
@@ -1646,7 +1685,7 @@ void tst_QStandardItemModel::taskQTBUG_45114_setItemData()
QModelIndex index = item->index();
QCOMPARE(model.itemData(index).size(), 3);
- QCOMPARE(spy.count(), 0);
+ QCOMPARE(spy.size(), 0);
QMap<int, QVariant> roles;
@@ -1654,21 +1693,21 @@ void tst_QStandardItemModel::taskQTBUG_45114_setItemData()
roles.insert(Qt::UserRole + 2, 2);
model.setItemData(index, roles);
- QCOMPARE(spy.count(), 0);
+ QCOMPARE(spy.size(), 0);
roles.insert(Qt::UserRole + 1, 1);
roles.insert(Qt::UserRole + 2, 2);
roles.insert(Qt::UserRole + 3, QVariant());
model.setItemData(index, roles);
- QCOMPARE(spy.count(), 0);
+ QCOMPARE(spy.size(), 0);
roles.clear();
roles.insert(Qt::UserRole + 1, 10);
roles.insert(Qt::UserRole + 3, 12);
model.setItemData(index, roles);
- QCOMPARE(spy.count(), 1);
+ QCOMPARE(spy.size(), 1);
QMap<int, QVariant> itemRoles = model.itemData(index);
QCOMPARE(itemRoles.size(), 4);
@@ -1680,13 +1719,13 @@ void tst_QStandardItemModel::taskQTBUG_45114_setItemData()
roles.insert(Qt::UserRole + 3, 1);
model.setItemData(index, roles);
- QCOMPARE(spy.count(), 2);
+ QCOMPARE(spy.size(), 2);
roles.clear();
roles.insert(Qt::UserRole + 3, QVariant());
model.setItemData(index, roles);
- QCOMPARE(spy.count(), 3);
+ QCOMPARE(spy.size(), 3);
itemRoles = model.itemData(index);
QCOMPARE(itemRoles.size(), 3);
@@ -1739,13 +1778,13 @@ void tst_QStandardItemModel::signalsOnTakeItem() // QTBUG-89145
QSignalSpy dataChangedSpy(&m, &QAbstractItemModel::dataChanged);
QStandardItem *const takenItem = m.takeItem(1, 0);
for (auto &&spy : removeSpies) {
- QCOMPARE(spy->count(), 1);
+ QCOMPARE(spy->size(), 1);
const auto spyArgs = spy->takeFirst();
QCOMPARE(spyArgs.at(0).value<QModelIndex>(), parentIndex);
QCOMPARE(spyArgs.at(1).toInt(), 0);
QCOMPARE(spyArgs.at(2).toInt(), 1);
}
- QCOMPARE(dataChangedSpy.count(), 1);
+ QCOMPARE(dataChangedSpy.size(), 1);
const auto dataChangedSpyArgs = dataChangedSpy.takeFirst();
QCOMPARE(dataChangedSpyArgs.at(0).value<QModelIndex>(), m.index(1, 0));
QCOMPARE(dataChangedSpyArgs.at(1).value<QModelIndex>(), m.index(1, 0));
@@ -1759,6 +1798,7 @@ void tst_QStandardItemModel::signalsOnTakeItem() // QTBUG-89145
QCOMPARE(takenItem->model(), nullptr);
QCOMPARE(takenItem->child(0, 0)->model(), nullptr);
QCOMPARE(m.index(1, 0).data(), QVariant());
+ delete takenItem;
}
void tst_QStandardItemModel::createPersistentOnLayoutAboutToBeChanged() // QTBUG-93466
@@ -1796,5 +1836,36 @@ void tst_QStandardItemModel::createPersistentOnLayoutAboutToBeChanged() // QTBUG
QCOMPARE(layoutChangedSpy.size(), 1);
}
+void tst_QStandardItemModel::takeChild() // QTBUG-117900
+{
+ {
+ // with model
+ QStandardItemModel model1;
+ QStandardItemModel model2;
+ QStandardItem base1("base1");
+ model1.setItem(0, 0, &base1);
+ QStandardItem base2("base2");
+ model2.setItem(0, 0, &base2);
+ auto item = new QStandardItem("item1");
+ item->appendRow(new QStandardItem("child"));
+ base1.appendRow(item);
+ base2.appendRow(base1.takeChild(0, 0));
+ QCOMPARE(base1.child(0, 0), nullptr);
+ QCOMPARE(base2.child(0, 0), item);
+ }
+ {
+ // without model
+ QStandardItem base1("base1");
+ QStandardItem base2("base2");
+ auto item = new QStandardItem("item1");
+ item->appendRow(new QStandardItem("child"));
+ base1.appendRow(item);
+ base2.appendRow(base1.takeChild(0, 0));
+ QCOMPARE(base1.child(0, 0), nullptr);
+ QCOMPARE(base2.child(0, 0), item);
+ }
+}
+
+
QTEST_MAIN(tst_QStandardItemModel)
#include "tst_qstandarditemmodel.moc"