Skip to content

Commit

Permalink
Deprecate Q(Persistent)ModelIndex::child
Browse files Browse the repository at this point in the history
Due to how invalid model indices are handled in Qt, child() is unsuitable
for general purpose usage. In particular you can never get a top level
item in the model because the root model index by definition hasn't got
a pointer to the model it belongs.

That makes child() useless for anything but tree models (and even there
you'd need to special case your code anyhow).

[ChangeLog][QtCore][QModelIndex] QModelIndex::child has
been deprecated due to its lack of generality.
Use model->index(row, column, index) instead.

[ChangeLog][QtCore][QPersistentModelIndex] QPersistentModelIndex::child has
been deprecated due to its lack of generality.
Use model->index(row, column, index) instead.

Change-Id: Ice73c17133aaf71355fa2af1eacfe64da01bd456
Reviewed-by: Thorbjørn Lund Martsum <[email protected]>
Reviewed-by: David Faure <[email protected]>
  • Loading branch information
dangelog committed Aug 16, 2016
1 parent 17198e0 commit 8dc45d5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
20 changes: 15 additions & 5 deletions src/corelib/itemmodels/qabstractitemmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ quintptr QPersistentModelIndex::internalId() const
Returns the parent QModelIndex for this persistent index, or an invalid
QModelIndex if it has no parent.
\sa child(), sibling(), model()
\sa sibling(), model()
*/
QModelIndex QPersistentModelIndex::parent() const
{
Expand All @@ -377,7 +377,7 @@ QModelIndex QPersistentModelIndex::parent() const
Returns the sibling at \a row and \a column or an invalid QModelIndex if
there is no sibling at this position.
\sa parent(), child()
\sa parent()
*/

QModelIndex QPersistentModelIndex::sibling(int row, int column) const
Expand All @@ -387,7 +387,12 @@ QModelIndex QPersistentModelIndex::sibling(int row, int column) const
return QModelIndex();
}

#if QT_DEPRECATED_SINCE(5, 8)
/*!
\obsolete
Use QAbstractItemModel::index() instead.
Returns the child of the model index that is stored in the given \a row
and \a column.
Expand All @@ -397,9 +402,10 @@ QModelIndex QPersistentModelIndex::sibling(int row, int column) const
QModelIndex QPersistentModelIndex::child(int row, int column) const
{
if (d)
return d->index.child(row, column);
return d->index.model()->index(row, column, d->index);
return QModelIndex();
}
#endif

/*!
Returns the data for the given \a role for the item referred to by the
Expand Down Expand Up @@ -1099,12 +1105,16 @@ 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(), child()
\sa parent()
*/

/*!
\fn QModelIndex QModelIndex::child(int row, int column) const
\obsolete
Use QAbstractItemModel::index() instead.
Returns the child of the model index that is stored in the given \a row and
\a column.
Expand Down Expand Up @@ -1153,7 +1163,7 @@ void QAbstractItemModel::resetInternalData()
Returns the parent of the model index, or QModelIndex() if it has no
parent.
\sa child(), sibling(), model()
\sa sibling(), model()
*/

/*!
Expand Down
10 changes: 8 additions & 2 deletions src/corelib/itemmodels/qabstractitemmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ class Q_CORE_EXPORT QModelIndex
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 child(int row, int column) const;
#if QT_DEPRECATED_SINCE(5, 8)
QT_DEPRECATED_X("Use QAbstractItemModel::index") inline QModelIndex child(int row, int column) const;
#endif
inline QVariant data(int role = Qt::DisplayRole) const;
inline Qt::ItemFlags flags() const;
Q_DECL_CONSTEXPR inline const QAbstractItemModel *model() const Q_DECL_NOTHROW { return m; }
Expand Down Expand Up @@ -128,7 +130,9 @@ class Q_CORE_EXPORT QPersistentModelIndex
quintptr internalId() const;
QModelIndex parent() const;
QModelIndex sibling(int row, int column) const;
QModelIndex child(int row, int column) const;
#if QT_DEPRECATED_SINCE(5, 8)
QT_DEPRECATED_X("Use QAbstractItemModel::index") QModelIndex child(int row, int column) const;
#endif
QVariant data(int role = Qt::DisplayRole) const;
Qt::ItemFlags flags() const;
const QAbstractItemModel *model() const;
Expand Down Expand Up @@ -419,8 +423,10 @@ 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(); }

#if QT_DEPRECATED_SINCE(5, 8)
inline QModelIndex QModelIndex::child(int arow, int acolumn) const
{ return m ? m->index(arow, acolumn, *this) : QModelIndex(); }
#endif

inline QVariant QModelIndex::data(int arole) const
{ return m ? m->data(*this, arole) : QVariant(); }
Expand Down

0 comments on commit 8dc45d5

Please sign in to comment.