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:
parent
5169d588a5
commit
52c66e8515
@ -1925,7 +1925,7 @@ void QAbstractItemView::mouseReleaseEvent(QMouseEvent *event)
|
|||||||
QStyleOptionViewItem option = d->viewOptionsV1();
|
QStyleOptionViewItem option = d->viewOptionsV1();
|
||||||
if (d->pressedAlreadySelected)
|
if (d->pressedAlreadySelected)
|
||||||
option.state |= QStyle::State_Selected;
|
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))
|
&& style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick, &option, this))
|
||||||
emit activated(index);
|
emit activated(index);
|
||||||
}
|
}
|
||||||
|
@ -153,6 +153,7 @@ private slots:
|
|||||||
void testClickToSelect();
|
void testClickToSelect();
|
||||||
void testDialogAsEditor();
|
void testDialogAsEditor();
|
||||||
void QTBUG46785_mouseout_hover_state();
|
void QTBUG46785_mouseout_hover_state();
|
||||||
|
void testClearModelInClickedSignal();
|
||||||
};
|
};
|
||||||
|
|
||||||
class MyAbstractItemDelegate : public QAbstractItemDelegate
|
class MyAbstractItemDelegate : public QAbstractItemDelegate
|
||||||
@ -2266,5 +2267,29 @@ void tst_QAbstractItemView::QTBUG46785_mouseout_hover_state()
|
|||||||
QTRY_VERIFY(delegate.m_paintedWithoutHover);
|
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)
|
QTEST_MAIN(tst_QAbstractItemView)
|
||||||
#include "tst_qabstractitemview.moc"
|
#include "tst_qabstractitemview.moc"
|
||||||
|
Loading…
Reference in New Issue
Block a user