From 75b5db3ce69e981132c86e90820d59becb874d4a Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Thu, 15 Jun 2017 16:43:53 +0200 Subject: [PATCH] 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 b2aeeaf628839d0fc0149db94e0a02c369ad16bc. Change-Id: I75d597867771f414821693a50f97c94b05a4ccb2 Reviewed-by: David Faure --- src/widgets/itemviews/qlistwidget.cpp | 19 +++++++++++++++++-- src/widgets/itemviews/qlistwidget.h | 5 ++++- src/widgets/itemviews/qlistwidget_p.h | 2 +- src/widgets/itemviews/qtablewidget.cpp | 16 +++++++++++++++- src/widgets/itemviews/qtablewidget.h | 5 ++++- src/widgets/itemviews/qtreewidget.cpp | 2 ++ src/widgets/itemviews/qtreewidget.h | 2 ++ 7 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/widgets/itemviews/qlistwidget.cpp b/src/widgets/itemviews/qlistwidget.cpp index 95ad3f82d0..c14dcec744 100644 --- a/src/widgets/itemviews/qlistwidget.cpp +++ b/src/widgets/itemviews/qlistwidget.cpp @@ -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(item_); if (!item || !item->view || static_cast(item->view->model()) != this || items.isEmpty()) return QModelIndex(); @@ -1942,14 +1943,28 @@ QList 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(item)); +} +#endif + /*! Returns a pointer to the QListWidgetItem associated with the given \a index. */ diff --git a/src/widgets/itemviews/qlistwidget.h b/src/widgets/itemviews/qlistwidget.h index 50f4e2ac84..3416bf83a7 100644 --- a/src/widgets/itemviews/qlistwidget.h +++ b/src/widgets/itemviews/qlistwidget.h @@ -297,7 +297,10 @@ protected: #endif QList 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: diff --git a/src/widgets/itemviews/qlistwidget_p.h b/src/widgets/itemviews/qlistwidget_p.h index e8f5540f9c..492b05ff07 100644 --- a/src/widgets/itemviews/qlistwidget_p.h +++ b/src/widgets/itemviews/qlistwidget_p.h @@ -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; diff --git a/src/widgets/itemviews/qtablewidget.cpp b/src/widgets/itemviews/qtablewidget.cpp index bb1970e3ac..a0c01e2f25 100644 --- a/src/widgets/itemviews/qtablewidget.cpp +++ b/src/widgets/itemviews/qtablewidget.cpp @@ -2652,14 +2652,28 @@ QList 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(item)); +} +#endif + /*! Returns a pointer to the QTableWidgetItem associated with the given \a index. */ diff --git a/src/widgets/itemviews/qtablewidget.h b/src/widgets/itemviews/qtablewidget.h index 9c231d5127..fa51546b50 100644 --- a/src/widgets/itemviews/qtablewidget.h +++ b/src/widgets/itemviews/qtablewidget.h @@ -335,7 +335,10 @@ protected: #endif QList 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: diff --git a/src/widgets/itemviews/qtreewidget.cpp b/src/widgets/itemviews/qtreewidget.cpp index d7b46a0835..ca5c662a9e 100644 --- a/src/widgets/itemviews/qtreewidget.cpp +++ b/src/widgets/itemviews/qtreewidget.cpp @@ -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(item), column); } +#endif /*! Returns a pointer to the QTreeWidgetItem associated with the given \a index. diff --git a/src/widgets/itemviews/qtreewidget.h b/src/widgets/itemviews/qtreewidget.h index 783627cde9..424ab597af 100644 --- a/src/widgets/itemviews/qtreewidget.h +++ b/src/widgets/itemviews/qtreewidget.h @@ -365,7 +365,9 @@ protected: QList 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: