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>
This commit is contained in:
Andre Somers 2018-01-03 10:24:06 +01:00
parent 7157d04d6e
commit abbd26f553
4 changed files with 39 additions and 3 deletions

View File

@ -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
*/
/*!

View File

@ -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(); }

View File

@ -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);
}
}

View File

@ -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);