QAbstractItemView: check the index emitted by the delegate's sizeHintChanged

The index in question has to belong to the view's model. An erroneous
emission shouldn't be silently ignored.

Change-Id: I7e037e72affb210f609a9a1029777acafae041f1
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Giuseppe D'Angelo 2022-04-26 00:36:32 +02:00
parent 5fd6704091
commit 9e8adb6464
3 changed files with 18 additions and 6 deletions

View File

@ -244,6 +244,16 @@ void QAbstractItemViewPrivate::_q_scrollerStateChanged()
#endif // QT_NO_GESTURES
void QAbstractItemViewPrivate::_q_delegateSizeHintChanged(const QModelIndex &index)
{
Q_Q(QAbstractItemView);
if (model) {
if (!model->checkIndex(index))
qWarning("Delegate size hint changed for a model index that does not belong to this view");
}
QMetaObject::invokeMethod(q, &QAbstractItemView::doItemsLayout, Qt::QueuedConnection);
}
/*!
\class QAbstractItemView
@ -853,7 +863,7 @@ void QAbstractItemView::setItemDelegate(QAbstractItemDelegate *delegate)
disconnect(d->itemDelegate, SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)),
this, SLOT(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)));
disconnect(d->itemDelegate, SIGNAL(commitData(QWidget*)), this, SLOT(commitData(QWidget*)));
disconnect(d->itemDelegate, SIGNAL(sizeHintChanged(QModelIndex)), this, SLOT(doItemsLayout()));
disconnect(d->itemDelegate, SIGNAL(sizeHintChanged(QModelIndex)), this, SLOT(_q_delegateSizeHintChanged(QModelIndex)));
}
}
@ -862,7 +872,7 @@ void QAbstractItemView::setItemDelegate(QAbstractItemDelegate *delegate)
connect(delegate, SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)),
this, SLOT(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)));
connect(delegate, SIGNAL(commitData(QWidget*)), this, SLOT(commitData(QWidget*)));
connect(delegate, SIGNAL(sizeHintChanged(QModelIndex)), this, SLOT(doItemsLayout()), Qt::QueuedConnection);
connect(delegate, SIGNAL(sizeHintChanged(QModelIndex)), this, SLOT(_q_delegateSizeHintChanged(QModelIndex)));
}
}
d->itemDelegate = delegate;
@ -937,7 +947,7 @@ void QAbstractItemView::setItemDelegateForRow(int row, QAbstractItemDelegate *de
disconnect(rowDelegate, SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)),
this, SLOT(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)));
disconnect(rowDelegate, SIGNAL(commitData(QWidget*)), this, SLOT(commitData(QWidget*)));
disconnect(rowDelegate, SIGNAL(sizeHintChanged(QModelIndex)), this, SLOT(doItemsLayout()));
disconnect(rowDelegate, SIGNAL(sizeHintChanged(QModelIndex)), this, SLOT(_q_delegateSizeHintChanged(QModelIndex)));
}
d->rowDelegates.remove(row);
}
@ -946,7 +956,7 @@ void QAbstractItemView::setItemDelegateForRow(int row, QAbstractItemDelegate *de
connect(delegate, SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)),
this, SLOT(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)));
connect(delegate, SIGNAL(commitData(QWidget*)), this, SLOT(commitData(QWidget*)));
connect(delegate, SIGNAL(sizeHintChanged(QModelIndex)), this, SLOT(doItemsLayout()), Qt::QueuedConnection);
connect(delegate, SIGNAL(sizeHintChanged(QModelIndex)), this, SLOT(_q_delegateSizeHintChanged(QModelIndex)));
}
d->rowDelegates.insert(row, delegate);
}
@ -997,7 +1007,7 @@ void QAbstractItemView::setItemDelegateForColumn(int column, QAbstractItemDelega
disconnect(columnDelegate, SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)),
this, SLOT(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)));
disconnect(columnDelegate, SIGNAL(commitData(QWidget*)), this, SLOT(commitData(QWidget*)));
disconnect(columnDelegate, SIGNAL(sizeHintChanged(QModelIndex)), this, SLOT(doItemsLayout()));
disconnect(columnDelegate, SIGNAL(sizeHintChanged(QModelIndex)), this, SLOT(_q_delegateSizeHintChanged(QModelIndex)));
}
d->columnDelegates.remove(column);
}
@ -1006,7 +1016,7 @@ void QAbstractItemView::setItemDelegateForColumn(int column, QAbstractItemDelega
connect(delegate, SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)),
this, SLOT(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)));
connect(delegate, SIGNAL(commitData(QWidget*)), this, SLOT(commitData(QWidget*)));
connect(delegate, SIGNAL(sizeHintChanged(QModelIndex)), this, SLOT(doItemsLayout()), Qt::QueuedConnection);
connect(delegate, SIGNAL(sizeHintChanged(QModelIndex)), this, SLOT(_q_delegateSizeHintChanged(QModelIndex)));
}
d->columnDelegates.insert(column, delegate);
}

View File

@ -375,6 +375,7 @@ private:
#if QT_CONFIG(gestures) && QT_CONFIG(scroller)
Q_PRIVATE_SLOT(d_func(), void _q_scrollerStateChanged())
#endif
Q_PRIVATE_SLOT(d_func(), void _q_delegateSizeHintChanged(const QModelIndex&))
friend class ::tst_QAbstractItemView;
friend class ::tst_QTreeView;

View File

@ -112,6 +112,7 @@ public:
void _q_headerDataChanged() { doDelayedItemsLayout(); }
void _q_scrollerStateChanged();
void _q_delegateSizeHintChanged(const QModelIndex &index);
void fetchMore();