QAbstractItemView: fix nullptr violation

If in a slot connected to QAbstractItemView::clicked
QAbstractItemView::setModel(nullptr) is called the method
QAbstractItemView::mouseReleaseEvent will cause a segmentation fault.
The problem is that the method QAbstractItemView::model used in
QAbstractItemView::mouseReleaseEvent will return a nullptr if a null
model was set. The solution is to used d->model since it is always a
valid model. (See line d->model =
(model ? model : QAbstractItemModelPrivate::staticEmptyModel());
in method QAbstractItemView::setModel)

Change-Id: I6f01bdeac64495ee4a76adcc7bf8da8a7719ef4d
Reviewed-by: David Faure <david.faure@kdab.com>
This commit is contained in:
Raschbauer, Reinhard 2017-05-17 11:03:02 +02:00 committed by Simon Hausmann
parent 5169d588a5
commit 52c66e8515
2 changed files with 26 additions and 1 deletions

View File

@ -1925,7 +1925,7 @@ void QAbstractItemView::mouseReleaseEvent(QMouseEvent *event)
QStyleOptionViewItem option = d->viewOptionsV1();
if (d->pressedAlreadySelected)
option.state |= QStyle::State_Selected;
if ((model()->flags(index) & Qt::ItemIsEnabled)
if ((d->model->flags(index) & Qt::ItemIsEnabled)
&& style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick, &option, this))
emit activated(index);
}

View File

@ -153,6 +153,7 @@ private slots:
void testClickToSelect();
void testDialogAsEditor();
void QTBUG46785_mouseout_hover_state();
void testClearModelInClickedSignal();
};
class MyAbstractItemDelegate : public QAbstractItemDelegate
@ -2266,5 +2267,29 @@ void tst_QAbstractItemView::QTBUG46785_mouseout_hover_state()
QTRY_VERIFY(delegate.m_paintedWithoutHover);
}
void tst_QAbstractItemView::testClearModelInClickedSignal()
{
QStringList list{"A", "B"};
QStringListModel model(list);
QListView view;
view.setModel(&model);
view.show();
QWidget::connect(&view, &QListView::clicked, [&view](const QModelIndex &index)
{
view.setModel(nullptr);
QCOMPARE(index.data().toString(), QStringLiteral("B"));
});
QModelIndex index = view.model()->index(1, 0);
QVERIFY(index.isValid());
QPoint p = view.visualRect(index).center();
QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::NoModifier, p);
QCOMPARE(view.model(), nullptr);
}
QTEST_MAIN(tst_QAbstractItemView)
#include "tst_qabstractitemview.moc"