summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndre Somers <andre.somers@kdab.com>2018-01-03 10:24:06 +0100
committerAndre Somers <andre.somers@kdab.com>2018-01-12 15:12:53 +0000
commitabbd26f553f9c96649a23b71b1580a23933ba5ac (patch)
treea0e067305c7263d54dc778400e0c864d177a328a
parent7157d04d6e2ee024519b3bdea1203a26e2cd41b8 (diff)
Add QModelIndex::siblingAtColumn and ::siblingAtRow
Data in item models is most often organized in rows, where each column contains an attribute of the item represented by the row. Often when sibling is used, it is to request another piece of data from the same row. Having a specialized version makes this easier and less awkward to do, simplifying auto sibling = index.sibling(index.row(), columnOfInterest); to auto sibling = index.siblingAtColumn(columnOfInterest); For symmetry reasons, siblingAtRow(rowOfInterest) was also added. Change-Id: Ib203b2cdb16154cbb2680d16fb5c6a7538f33d07 Reviewed-by: Topi Reiniƶ <topi.reinio@qt.io> Reviewed-by: David Faure <david.faure@kdab.com>
-rw-r--r--src/corelib/itemmodels/qabstractitemmodel.cpp22
-rw-r--r--src/corelib/itemmodels/qabstractitemmodel.h8
-rw-r--r--tests/auto/corelib/itemmodels/qabstractproxymodel/tst_qabstractproxymodel.cpp4
-rw-r--r--tests/auto/corelib/itemmodels/qitemmodel/tst_qitemmodel.cpp8
4 files changed, 39 insertions, 3 deletions
diff --git a/src/corelib/itemmodels/qabstractitemmodel.cpp b/src/corelib/itemmodels/qabstractitemmodel.cpp
index af87f56255..758ac4bba6 100644
--- a/src/corelib/itemmodels/qabstractitemmodel.cpp
+++ b/src/corelib/itemmodels/qabstractitemmodel.cpp
@@ -1108,7 +1108,27 @@ void QAbstractItemModel::resetInternalData()
Returns the sibling at \a row and \a column. If there is no sibling at this
position, an invalid QModelIndex is returned.
- \sa parent()
+ \sa parent(), siblingAtColumn(), siblingAtRow()
+*/
+
+/*!
+ \fn QModelIndex QModelIndex::siblingAtColumn(int column) const
+
+ Returns the sibling at \a column for the current row. If there is no sibling
+ at this position, an invalid QModelIndex is returned.
+
+ \sa sibling(), siblingAtRow()
+ \since 5.11
+*/
+
+/*!
+ \fn QModelIndex QModelIndex::siblingAtRow(int row) const
+
+ Returns the sibling at \a row for the current column. If there is no sibling
+ at this position, an invalid QModelIndex is returned.
+
+ \sa sibling(), siblingAtColumn()
+ \since 5.11
*/
/*!
diff --git a/src/corelib/itemmodels/qabstractitemmodel.h b/src/corelib/itemmodels/qabstractitemmodel.h
index 621f284c34..fca21b9bbc 100644
--- a/src/corelib/itemmodels/qabstractitemmodel.h
+++ b/src/corelib/itemmodels/qabstractitemmodel.h
@@ -63,6 +63,8 @@ public:
inline void *internalPointer() const Q_DECL_NOTHROW { return reinterpret_cast<void*>(i); }
inline QModelIndex parent() const;
inline QModelIndex sibling(int row, int column) const;
+ inline QModelIndex siblingAtColumn(int column) const;
+ inline QModelIndex siblingAtRow(int row) const;
#if QT_DEPRECATED_SINCE(5, 8)
QT_DEPRECATED_X("Use QAbstractItemModel::index") inline QModelIndex child(int row, int column) const;
#endif
@@ -436,6 +438,12 @@ inline QModelIndex QModelIndex::parent() const
inline QModelIndex QModelIndex::sibling(int arow, int acolumn) const
{ return m ? (r == arow && c == acolumn) ? *this : m->sibling(arow, acolumn, *this) : QModelIndex(); }
+inline QModelIndex QModelIndex::siblingAtColumn(int acolumn) const
+{ return m ? (c == acolumn) ? *this : m->sibling(r, acolumn, *this) : QModelIndex(); }
+
+inline QModelIndex QModelIndex::siblingAtRow(int arow) const
+{ return m ? (r == arow) ? *this : m->sibling(arow, c, *this) : QModelIndex(); }
+
#if QT_DEPRECATED_SINCE(5, 8)
inline QModelIndex QModelIndex::child(int arow, int acolumn) const
{ return m ? m->index(arow, acolumn, *this) : QModelIndex(); }
diff --git a/tests/auto/corelib/itemmodels/qabstractproxymodel/tst_qabstractproxymodel.cpp b/tests/auto/corelib/itemmodels/qabstractproxymodel/tst_qabstractproxymodel.cpp
index 6c870737da..886941bff6 100644
--- a/tests/auto/corelib/itemmodels/qabstractproxymodel/tst_qabstractproxymodel.cpp
+++ b/tests/auto/corelib/itemmodels/qabstractproxymodel/tst_qabstractproxymodel.cpp
@@ -477,8 +477,8 @@ void tst_QAbstractProxyModel::testSwappingRowsProxy()
for (int row = 0; row < defaultModel.rowCount(); ++row) {
QModelIndex left = proxy.index(row, 0, QModelIndex());
QModelIndex right = proxy.index(row, 1, QModelIndex());
- QCOMPARE(left.sibling(left.row(), 1), right);
- QCOMPARE(right.sibling(right.row(), 0), left);
+ QCOMPARE(left.siblingAtColumn(1), right);
+ QCOMPARE(right.siblingAtColumn(0), left);
}
}
diff --git a/tests/auto/corelib/itemmodels/qitemmodel/tst_qitemmodel.cpp b/tests/auto/corelib/itemmodels/qitemmodel/tst_qitemmodel.cpp
index 3d9446965e..7cd220e684 100644
--- a/tests/auto/corelib/itemmodels/qitemmodel/tst_qitemmodel.cpp
+++ b/tests/auto/corelib/itemmodels/qitemmodel/tst_qitemmodel.cpp
@@ -428,6 +428,14 @@ void checkChildren(QAbstractItemModel *currentModel, const QModelIndex &parent,
const QModelIndex sibling = topLeftChild.sibling( r, c );
QVERIFY( index == sibling );
}
+ if (r == topLeftChild.row()) {
+ const QModelIndex sibling = topLeftChild.siblingAtColumn( c );
+ QVERIFY( index == sibling );
+ }
+ if (c == topLeftChild.column()) {
+ const QModelIndex sibling = topLeftChild.siblingAtRow( r );
+ QVERIFY( index == sibling );
+ }
// Some basic checking on the index that is returned
QCOMPARE(index.model(), currentModel);