Widgets/Itemviews: use pmf-style connect in QTreeView
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: Ic6617b3611a2ce156e9e568c1d07343f649562c4 Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
This commit is contained in:
parent
3e144bdc74
commit
0bb35ee2f0
@ -169,6 +169,8 @@ QTreeView::QTreeView(QTreeViewPrivate &dd, QWidget *parent)
|
|||||||
*/
|
*/
|
||||||
QTreeView::~QTreeView()
|
QTreeView::~QTreeView()
|
||||||
{
|
{
|
||||||
|
Q_D(QTreeView);
|
||||||
|
d->clearConnections();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@ -180,18 +182,12 @@ void QTreeView::setModel(QAbstractItemModel *model)
|
|||||||
if (model == d->model)
|
if (model == d->model)
|
||||||
return;
|
return;
|
||||||
if (d->model && d->model != QAbstractItemModelPrivate::staticEmptyModel()) {
|
if (d->model && d->model != QAbstractItemModelPrivate::staticEmptyModel()) {
|
||||||
disconnect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
|
for (const QMetaObject::Connection &connection : d->modelConnections)
|
||||||
this, SLOT(rowsRemoved(QModelIndex,int,int)));
|
QObject::disconnect(connection);
|
||||||
|
|
||||||
disconnect(d->model, SIGNAL(modelAboutToBeReset()), this, SLOT(_q_modelAboutToBeReset()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (d->selectionModel) { // support row editing
|
if (d->selectionModel) { // support row editing
|
||||||
disconnect(d->selectionModel, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
|
QObject::disconnect(d->selectionmodelConnection);
|
||||||
d->model, SLOT(submit()));
|
|
||||||
disconnect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
|
|
||||||
this, SLOT(rowsRemoved(QModelIndex,int,int)));
|
|
||||||
disconnect(d->model, SIGNAL(modelAboutToBeReset()), this, SLOT(_q_modelAboutToBeReset()));
|
|
||||||
}
|
}
|
||||||
d->viewItems.clear();
|
d->viewItems.clear();
|
||||||
d->expandedIndexes.clear();
|
d->expandedIndexes.clear();
|
||||||
@ -201,18 +197,22 @@ void QTreeView::setModel(QAbstractItemModel *model)
|
|||||||
d->geometryRecursionBlock = false;
|
d->geometryRecursionBlock = false;
|
||||||
QAbstractItemView::setModel(model);
|
QAbstractItemView::setModel(model);
|
||||||
|
|
||||||
|
if (d->model) {
|
||||||
// QAbstractItemView connects to a private slot
|
// QAbstractItemView connects to a private slot
|
||||||
disconnect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
|
QObjectPrivate::disconnect(d->model, &QAbstractItemModel::rowsRemoved,
|
||||||
this, SLOT(_q_rowsRemoved(QModelIndex,int,int)));
|
d, &QAbstractItemViewPrivate::_q_rowsRemoved);
|
||||||
// do header layout after the tree
|
// do header layout after the tree
|
||||||
disconnect(d->model, SIGNAL(layoutChanged()),
|
QObjectPrivate::disconnect(d->model, &QAbstractItemModel::layoutChanged,
|
||||||
d->header, SLOT(_q_layoutChanged()));
|
d->header->d_func(), &QAbstractItemViewPrivate::_q_layoutChanged);
|
||||||
|
|
||||||
|
d->modelConnections = {
|
||||||
// QTreeView has a public slot for this
|
// QTreeView has a public slot for this
|
||||||
connect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
|
QObject::connect(d->model, &QAbstractItemModel::rowsRemoved,
|
||||||
this, SLOT(rowsRemoved(QModelIndex,int,int)));
|
this, &QTreeView::rowsRemoved),
|
||||||
|
QObjectPrivate::connect(d->model, &QAbstractItemModel::modelAboutToBeReset,
|
||||||
connect(d->model, SIGNAL(modelAboutToBeReset()), SLOT(_q_modelAboutToBeReset()));
|
d, &QTreeViewPrivate::_q_modelAboutToBeReset)
|
||||||
|
};
|
||||||
|
}
|
||||||
if (d->sortingEnabled)
|
if (d->sortingEnabled)
|
||||||
d->_q_sortIndicatorChanged(header()->sortIndicatorSection(), header()->sortIndicatorOrder());
|
d->_q_sortIndicatorChanged(header()->sortIndicatorSection(), header()->sortIndicatorOrder());
|
||||||
}
|
}
|
||||||
@ -236,8 +236,7 @@ void QTreeView::setSelectionModel(QItemSelectionModel *selectionModel)
|
|||||||
Q_ASSERT(selectionModel);
|
Q_ASSERT(selectionModel);
|
||||||
if (d->selectionModel) {
|
if (d->selectionModel) {
|
||||||
// support row editing
|
// support row editing
|
||||||
disconnect(d->selectionModel, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
|
QObject::disconnect(d->selectionmodelConnection);
|
||||||
d->model, SLOT(submit()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
d->header->setSelectionModel(selectionModel);
|
d->header->setSelectionModel(selectionModel);
|
||||||
@ -245,8 +244,9 @@ void QTreeView::setSelectionModel(QItemSelectionModel *selectionModel)
|
|||||||
|
|
||||||
if (d->selectionModel) {
|
if (d->selectionModel) {
|
||||||
// support row editing
|
// support row editing
|
||||||
connect(d->selectionModel, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
|
d->selectionmodelConnection =
|
||||||
d->model, SLOT(submit()));
|
connect(d->selectionModel, &QItemSelectionModel::currentRowChanged,
|
||||||
|
d->model, &QAbstractItemModel::submit);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -286,16 +286,18 @@ void QTreeView::setHeader(QHeaderView *header)
|
|||||||
d->header->setSelectionModel(d->selectionModel);
|
d->header->setSelectionModel(d->selectionModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
connect(d->header, SIGNAL(sectionResized(int,int,int)),
|
d->headerConnections = {
|
||||||
this, SLOT(columnResized(int,int,int)));
|
connect(d->header, &QHeaderView::sectionResized,
|
||||||
connect(d->header, SIGNAL(sectionMoved(int,int,int)),
|
this, &QTreeView::columnResized),
|
||||||
this, SLOT(columnMoved()));
|
connect(d->header, &QHeaderView::sectionMoved,
|
||||||
connect(d->header, SIGNAL(sectionCountChanged(int,int)),
|
this, &QTreeView::columnMoved),
|
||||||
this, SLOT(columnCountChanged(int,int)));
|
connect(d->header, &QHeaderView::sectionCountChanged,
|
||||||
connect(d->header, SIGNAL(sectionHandleDoubleClicked(int)),
|
this, &QTreeView::columnCountChanged),
|
||||||
this, SLOT(resizeColumnToContents(int)));
|
connect(d->header, &QHeaderView::sectionHandleDoubleClicked,
|
||||||
connect(d->header, SIGNAL(geometriesChanged()),
|
this, &QTreeView::resizeColumnToContents),
|
||||||
this, SLOT(updateGeometries()));
|
connect(d->header, &QHeaderView::geometriesChanged,
|
||||||
|
this, &QTreeView::updateGeometries)
|
||||||
|
};
|
||||||
|
|
||||||
setSortingEnabled(d->sortingEnabled);
|
setSortingEnabled(d->sortingEnabled);
|
||||||
d->updateGeometry();
|
d->updateGeometry();
|
||||||
@ -839,11 +841,12 @@ void QTreeView::setSortingEnabled(bool enable)
|
|||||||
//sortByColumn has to be called before we connect or set the sortingEnabled flag
|
//sortByColumn has to be called before we connect or set the sortingEnabled flag
|
||||||
// because otherwise it will not call sort on the model.
|
// because otherwise it will not call sort on the model.
|
||||||
sortByColumn(header()->sortIndicatorSection(), header()->sortIndicatorOrder());
|
sortByColumn(header()->sortIndicatorSection(), header()->sortIndicatorOrder());
|
||||||
connect(header(), SIGNAL(sortIndicatorChanged(int,Qt::SortOrder)),
|
d->sortHeaderConnection =
|
||||||
this, SLOT(_q_sortIndicatorChanged(int,Qt::SortOrder)), Qt::UniqueConnection);
|
QObjectPrivate::connect(header(), &QHeaderView::sortIndicatorChanged,
|
||||||
|
d, &QTreeViewPrivate::_q_sortIndicatorChanged,
|
||||||
|
Qt::UniqueConnection);
|
||||||
} else {
|
} else {
|
||||||
disconnect(header(), SIGNAL(sortIndicatorChanged(int,Qt::SortOrder)),
|
QObject::disconnect(d->sortHeaderConnection);
|
||||||
this, SLOT(_q_sortIndicatorChanged(int,Qt::SortOrder)));
|
|
||||||
}
|
}
|
||||||
d->sortingEnabled = enable;
|
d->sortingEnabled = enable;
|
||||||
}
|
}
|
||||||
@ -3088,10 +3091,25 @@ void QTreeViewPrivate::initialize()
|
|||||||
q->setHeader(header);
|
q->setHeader(header);
|
||||||
#if QT_CONFIG(animation)
|
#if QT_CONFIG(animation)
|
||||||
animationsEnabled = q->style()->styleHint(QStyle::SH_Widget_Animation_Duration, nullptr, q) > 0;
|
animationsEnabled = q->style()->styleHint(QStyle::SH_Widget_Animation_Duration, nullptr, q) > 0;
|
||||||
QObject::connect(&animatedOperation, SIGNAL(finished()), q, SLOT(_q_endAnimatedOperation()));
|
animationConnection =
|
||||||
|
QObjectPrivate::connect(&animatedOperation, &QVariantAnimation::finished,
|
||||||
|
this, &QTreeViewPrivate::_q_endAnimatedOperation);
|
||||||
#endif // animation
|
#endif // animation
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void QTreeViewPrivate::clearConnections()
|
||||||
|
{
|
||||||
|
for (const QMetaObject::Connection &connection : modelConnections)
|
||||||
|
QObject::disconnect(connection);
|
||||||
|
for (const QMetaObject::Connection &connection : headerConnections)
|
||||||
|
QObject::disconnect(connection);
|
||||||
|
QObject::disconnect(selectionmodelConnection);
|
||||||
|
QObject::disconnect(sortHeaderConnection);
|
||||||
|
#if QT_CONFIG(animation)
|
||||||
|
QObject::disconnect(animationConnection);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
void QTreeViewPrivate::expand(int item, bool emitSignal)
|
void QTreeViewPrivate::expand(int item, bool emitSignal)
|
||||||
{
|
{
|
||||||
Q_Q(QTreeView);
|
Q_Q(QTreeView);
|
||||||
|
@ -23,6 +23,8 @@
|
|||||||
#include <QtCore/qvariantanimation.h>
|
#include <QtCore/qvariantanimation.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
|
||||||
QT_REQUIRE_CONFIG(treeview);
|
QT_REQUIRE_CONFIG(treeview);
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
@ -62,6 +64,7 @@ public:
|
|||||||
|
|
||||||
~QTreeViewPrivate() {}
|
~QTreeViewPrivate() {}
|
||||||
void initialize();
|
void initialize();
|
||||||
|
void clearConnections();
|
||||||
int logicalIndexForTree() const;
|
int logicalIndexForTree() const;
|
||||||
inline bool isTreePosition(int logicalIndex) const
|
inline bool isTreePosition(int logicalIndex) const
|
||||||
{
|
{
|
||||||
@ -254,6 +257,12 @@ public:
|
|||||||
|
|
||||||
// tree position
|
// tree position
|
||||||
int treePosition;
|
int treePosition;
|
||||||
|
|
||||||
|
QMetaObject::Connection animationConnection;
|
||||||
|
QMetaObject::Connection selectionmodelConnection;
|
||||||
|
std::array<QMetaObject::Connection, 2> modelConnections;
|
||||||
|
std::array<QMetaObject::Connection, 5> headerConnections;
|
||||||
|
QMetaObject::Connection sortHeaderConnection;
|
||||||
};
|
};
|
||||||
|
|
||||||
QT_END_NAMESPACE
|
QT_END_NAMESPACE
|
||||||
|
Loading…
Reference in New Issue
Block a user