Item widgets: make indexFromItem const-correct

The item is not supposed to be modified, so it should be passed
as pointer-to-const. A similar change was already done for
QTreeWidget in b2aeeaf628.

Change-Id: I75d597867771f414821693a50f97c94b05a4ccb2
Reviewed-by: David Faure <david.faure@kdab.com>
This commit is contained in:
Giuseppe D'Angelo 2017-06-15 16:43:53 +02:00
parent 4fa90c1757
commit 75b5db3ce6
7 changed files with 45 additions and 6 deletions

View File

@ -189,8 +189,9 @@ int QListModel::rowCount(const QModelIndex &parent) const
return parent.isValid() ? 0 : items.count();
}
QModelIndex QListModel::index(QListWidgetItem *item) const
QModelIndex QListModel::index(const QListWidgetItem *item_) const
{
QListWidgetItem *item = const_cast<QListWidgetItem *>(item_);
if (!item || !item->view || static_cast<const QListModel *>(item->view->model()) != this
|| items.isEmpty())
return QModelIndex();
@ -1942,14 +1943,28 @@ QList<QListWidgetItem*> QListWidget::items(const QMimeData *data) const
/*!
Returns the QModelIndex associated with the given \a item.
\note In Qt versions prior to 5.10, this function took a non-\c{const} \a item.
*/
QModelIndex QListWidget::indexFromItem(QListWidgetItem *item) const
QModelIndex QListWidget::indexFromItem(const QListWidgetItem *item) const
{
Q_D(const QListWidget);
return d->listModel()->index(item);
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
/*!
\internal
\obsolete
\overload
*/
QModelIndex QListWidget::indexFromItem(QListWidgetItem *item) const
{
return indexFromItem(const_cast<const QListWidgetItem *>(item));
}
#endif
/*!
Returns a pointer to the QListWidgetItem associated with the given \a index.
*/

View File

@ -297,7 +297,10 @@ protected:
#endif
QList<QListWidgetItem*> items(const QMimeData *data) const;
QModelIndex indexFromItem(QListWidgetItem *item) const;
QModelIndex indexFromItem(const QListWidgetItem *item) const;
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QModelIndex indexFromItem(QListWidgetItem *item) const; // ### Qt 6: remove
#endif
QListWidgetItem *itemFromIndex(const QModelIndex &index) const;
private:

View File

@ -95,7 +95,7 @@ public:
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QModelIndex index(QListWidgetItem *item) const;
QModelIndex index(const QListWidgetItem *item) const;
QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;

View File

@ -2652,14 +2652,28 @@ QList<QTableWidgetItem*> QTableWidget::items(const QMimeData *data) const
/*!
Returns the QModelIndex associated with the given \a item.
\note In Qt versions prior to 5.10, this function took a non-\c{const} \a item.
*/
QModelIndex QTableWidget::indexFromItem(QTableWidgetItem *item) const
QModelIndex QTableWidget::indexFromItem(const QTableWidgetItem *item) const
{
Q_D(const QTableWidget);
return d->tableModel()->index(item);
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
/*!
\internal
\obsolete
\overload
*/
QModelIndex QTableWidget::indexFromItem(QTableWidgetItem *item) const
{
return indexFromItem(const_cast<const QTableWidgetItem *>(item));
}
#endif
/*!
Returns a pointer to the QTableWidgetItem associated with the given \a index.
*/

View File

@ -335,7 +335,10 @@ protected:
#endif
QList<QTableWidgetItem*> items(const QMimeData *data) const;
QModelIndex indexFromItem(QTableWidgetItem *item) const;
QModelIndex indexFromItem(const QTableWidgetItem *item) const;
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QModelIndex indexFromItem(QTableWidgetItem *item) const; // ### Qt 6: remove
#endif
QTableWidgetItem *itemFromIndex(const QModelIndex &index) const;
protected:

View File

@ -3366,6 +3366,7 @@ QModelIndex QTreeWidget::indexFromItem(const QTreeWidgetItem *item, int column)
return d->index(item, column);
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
/*!
\overload
\internal
@ -3374,6 +3375,7 @@ QModelIndex QTreeWidget::indexFromItem(QTreeWidgetItem *item, int column) const
{
return indexFromItem(const_cast<const QTreeWidgetItem *>(item), column);
}
#endif
/*!
Returns a pointer to the QTreeWidgetItem associated with the given \a index.

View File

@ -365,7 +365,9 @@ protected:
QList<QTreeWidgetItem*> items(const QMimeData *data) const;
QModelIndex indexFromItem(const QTreeWidgetItem *item, int column = 0) const;
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QModelIndex indexFromItem(QTreeWidgetItem *item, int column = 0) const; // ### Qt 6: remove
#endif
QTreeWidgetItem *itemFromIndex(const QModelIndex &index) const;
protected: