From 063e25e5129ee8e383417b3671447c2eda14bcf8 Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Sun, 24 Sep 2023 19:43:41 +0200 Subject: [PATCH] Widgets/Itemviews: use pmf-style connect in QListWidget Replace all connect() calls with pmf-style connection syntax. This also means that we have to properly disconnect everything in the ctor to not trigger an assertion in QtPrivate::assertObjectType(). Task-number: QTBUG-117698 Change-Id: If2c259c3f7737cec736c134c8bc83c4cb14906ba Reviewed-by: Axel Spoerl --- src/widgets/itemviews/qlistwidget.cpp | 58 +++++++++++++++++---------- src/widgets/itemviews/qlistwidget_p.h | 6 +++ 2 files changed, 42 insertions(+), 22 deletions(-) diff --git a/src/widgets/itemviews/qlistwidget.cpp b/src/widgets/itemviews/qlistwidget.cpp index 695cca049e..130d2b02d9 100644 --- a/src/widgets/itemviews/qlistwidget.cpp +++ b/src/widgets/itemviews/qlistwidget.cpp @@ -1084,18 +1084,32 @@ void QListWidgetPrivate::setup() Q_Q(QListWidget); q->QListView::setModel(new QListModel(q)); // view signals - QObject::connect(q, SIGNAL(pressed(QModelIndex)), q, SLOT(_q_emitItemPressed(QModelIndex))); - QObject::connect(q, SIGNAL(clicked(QModelIndex)), q, SLOT(_q_emitItemClicked(QModelIndex))); - QObject::connect(q, SIGNAL(doubleClicked(QModelIndex)), - q, SLOT(_q_emitItemDoubleClicked(QModelIndex))); - QObject::connect(q, SIGNAL(activated(QModelIndex)), - q, SLOT(_q_emitItemActivated(QModelIndex))); - QObject::connect(q, SIGNAL(entered(QModelIndex)), q, SLOT(_q_emitItemEntered(QModelIndex))); - QObject::connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), - q, SLOT(_q_emitItemChanged(QModelIndex))); - QObject::connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), - q, SLOT(_q_dataChanged(QModelIndex,QModelIndex))); - QObject::connect(model, SIGNAL(columnsRemoved(QModelIndex,int,int)), q, SLOT(_q_sort())); + connections = { + QObjectPrivate::connect(q, &QListWidget::pressed, + this, &QListWidgetPrivate::_q_emitItemPressed), + QObjectPrivate::connect(q, &QListWidget::clicked, + this, &QListWidgetPrivate::_q_emitItemClicked), + QObjectPrivate::connect(q, &QListWidget::doubleClicked, + this, &QListWidgetPrivate::_q_emitItemDoubleClicked), + QObjectPrivate::connect(q, &QListWidget::activated, + this, &QListWidgetPrivate::_q_emitItemActivated), + QObjectPrivate::connect(q, &QListWidget::entered, + this, &QListWidgetPrivate::_q_emitItemEntered), + QObjectPrivate::connect(model, &QAbstractItemModel::dataChanged, + this, &QListWidgetPrivate::_q_emitItemChanged), + QObjectPrivate::connect(model, &QAbstractItemModel::dataChanged, + this, &QListWidgetPrivate::_q_dataChanged), + QObjectPrivate::connect(model, &QAbstractItemModel::columnsRemoved, + this, &QListWidgetPrivate::_q_sort) + }; +} + +void QListWidgetPrivate::clearConnections() +{ + for (const QMetaObject::Connection &connection : connections) + QObject::disconnect(connection); + for (const QMetaObject::Connection &connection : selectionModelConnections) + QObject::disconnect(connection); } void QListWidgetPrivate::_q_emitItemPressed(const QModelIndex &index) @@ -1365,6 +1379,8 @@ QListWidget::QListWidget(QWidget *parent) QListWidget::~QListWidget() { + Q_D(QListWidget); + d->clearConnections(); } /*! @@ -1375,20 +1391,18 @@ void QListWidget::setSelectionModel(QItemSelectionModel *selectionModel) { Q_D(QListWidget); - if (d->selectionModel) { - QObject::disconnect(d->selectionModel, SIGNAL(currentChanged(QModelIndex,QModelIndex)), - this, SLOT(_q_emitCurrentItemChanged(QModelIndex,QModelIndex))); - QObject::disconnect(d->selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)), - this, SIGNAL(itemSelectionChanged())); - } + for (const QMetaObject::Connection &connection : d->selectionModelConnections) + disconnect(connection); QListView::setSelectionModel(selectionModel); if (d->selectionModel) { - QObject::connect(d->selectionModel, SIGNAL(currentChanged(QModelIndex,QModelIndex)), - this, SLOT(_q_emitCurrentItemChanged(QModelIndex,QModelIndex))); - QObject::connect(d->selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)), - this, SIGNAL(itemSelectionChanged())); + d->selectionModelConnections = { + QObjectPrivate::connect(d->selectionModel, &QItemSelectionModel::currentChanged, + d, &QListWidgetPrivate::_q_emitCurrentItemChanged), + QObject::connect(d->selectionModel, &QItemSelectionModel::selectionChanged, + this, &QListWidget::itemSelectionChanged) + }; } } diff --git a/src/widgets/itemviews/qlistwidget_p.h b/src/widgets/itemviews/qlistwidget_p.h index edcacf0436..5516e7ca49 100644 --- a/src/widgets/itemviews/qlistwidget_p.h +++ b/src/widgets/itemviews/qlistwidget_p.h @@ -22,6 +22,8 @@ #include #include +#include + QT_REQUIRE_CONFIG(listwidget); QT_BEGIN_NAMESPACE @@ -113,6 +115,7 @@ public: QListWidgetPrivate() : QListViewPrivate(), sortOrder(Qt::AscendingOrder), sortingEnabled(false) {} inline QListModel *listModel() const { return qobject_cast(model); } void setup(); + void clearConnections(); void _q_emitItemPressed(const QModelIndex &index); void _q_emitItemClicked(const QModelIndex &index); void _q_emitItemDoubleClicked(const QModelIndex &index); @@ -124,6 +127,9 @@ public: void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight); Qt::SortOrder sortOrder; bool sortingEnabled; + + std::array connections; + std::array selectionModelConnections; }; class QListWidgetItemPrivate