From 3e2ee19553c2423e1e76264f3c3eb088cd31747a Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Tue, 9 Jun 2009 23:52:27 +0300 Subject: Made QListWidgetItem::operator<() check if the data is numerical when comparing. Merge-request: 631 Reviewed-by: Olivier Goffart --- src/gui/itemviews/qlistwidget.cpp | 5 +++- tests/auto/qlistwidget/tst_qlistwidget.cpp | 39 +++++++++++++++++++++--------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/gui/itemviews/qlistwidget.cpp b/src/gui/itemviews/qlistwidget.cpp index e1e509d..504908f 100644 --- a/src/gui/itemviews/qlistwidget.cpp +++ b/src/gui/itemviews/qlistwidget.cpp @@ -687,7 +687,10 @@ QVariant QListWidgetItem::data(int role) const */ bool QListWidgetItem::operator<(const QListWidgetItem &other) const { - return text() < other.text(); + const QVariant v1 = data(Qt::DisplayRole), v2 = other.data(Qt::DisplayRole); + if (QAbstractItemModelPrivate::canConvertToDouble(v1) && QAbstractItemModelPrivate::canConvertToDouble(v2)) + return v1.toDouble() < v2.toDouble(); + return v1.toString() < v2.toString(); } #ifndef QT_NO_DATASTREAM diff --git a/tests/auto/qlistwidget/tst_qlistwidget.cpp b/tests/auto/qlistwidget/tst_qlistwidget.cpp index 8d15aa4..8468cf3 100644 --- a/tests/auto/qlistwidget/tst_qlistwidget.cpp +++ b/tests/auto/qlistwidget/tst_qlistwidget.cpp @@ -883,31 +883,46 @@ void tst_QListWidget::itemStreaming() void tst_QListWidget::sortItems_data() { QTest::addColumn("order"); - QTest::addColumn("initialList"); - QTest::addColumn("expectedList"); + QTest::addColumn("initialList"); + QTest::addColumn("expectedList"); QTest::addColumn("expectedRows"); - QTest::newRow("ascending order") + QTest::newRow("ascending strings") << static_cast(Qt::AscendingOrder) - << (QStringList() << "c" << "d" << "a" << "b") - << (QStringList() << "a" << "b" << "c" << "d") + << (QVariantList() << QString("c") << QString("d") << QString("a") << QString("b")) + << (QVariantList() << QString("a") << QString("b") << QString("c") << QString("d")) << (IntList() << 2 << 3 << 0 << 1); - QTest::newRow("descending order") + QTest::newRow("descending strings") << static_cast(Qt::DescendingOrder) - << (QStringList() << "c" << "d" << "a" << "b") - << (QStringList() << "d" << "c" << "b" << "a") + << (QVariantList() << QString("c") << QString("d") << QString("a") << QString("b")) + << (QVariantList() << QString("d") << QString("c") << QString("b") << QString("a")) << (IntList() << 1 << 0 << 3 << 2); + + QTest::newRow("ascending numbers") + << static_cast(Qt::AscendingOrder) + << (QVariantList() << 1 << 11 << 2 << 22) + << (QVariantList() << 1 << 2 << 11 << 22) + << (IntList() << 0 << 2 << 1 << 3); + + QTest::newRow("descending numbers") + << static_cast(Qt::DescendingOrder) + << (QVariantList() << 1 << 11 << 2 << 22) + << (QVariantList() << 22 << 11 << 2 << 1) + << (IntList() << 3 << 1 << 2 << 0); } void tst_QListWidget::sortItems() { QFETCH(int, order); - QFETCH(QStringList, initialList); - QFETCH(QStringList, expectedList); + QFETCH(QVariantList, initialList); + QFETCH(QVariantList, expectedList); QFETCH(IntList, expectedRows); - testWidget->addItems(initialList); + foreach (const QVariant &data, initialList) { + QListWidgetItem *item = new QListWidgetItem(testWidget); + item->setData(Qt::DisplayRole, data); + } QAbstractItemModel *model = testWidget->model(); QList persistent; @@ -918,7 +933,7 @@ void tst_QListWidget::sortItems() QCOMPARE(testWidget->count(), expectedList.count()); for (int i = 0; i < testWidget->count(); ++i) - QCOMPARE(testWidget->item(i)->text(), expectedList.at(i)); + QCOMPARE(testWidget->item(i)->text(), expectedList.at(i).toString()); for (int k = 0; k < testWidget->count(); ++k) QCOMPARE(persistent.at(k).row(), expectedRows.at(k)); -- cgit v0.12