BaseTreeView: Robustify

Check for selection and model before accessing them.

Change-Id: If8e8e43f00c5812bce7a21639c3d3bee459117f3
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
This commit is contained in:
hjk
2018-07-24 10:44:31 +02:00
parent 48450e6a4d
commit c23980c72a

View File

@@ -309,6 +309,7 @@ void BaseTreeView::setModel(QAbstractItemModel *m)
void BaseTreeView::mousePressEvent(QMouseEvent *ev) void BaseTreeView::mousePressEvent(QMouseEvent *ev)
{ {
ItemViewEvent ive(ev, this); ItemViewEvent ive(ev, this);
QTC_ASSERT(model(), return);
if (!model()->setData(ive.index(), QVariant::fromValue(ive), ItemViewEventRole)) if (!model()->setData(ive.index(), QVariant::fromValue(ive), ItemViewEventRole))
TreeView::mousePressEvent(ev); TreeView::mousePressEvent(ev);
// Resizing columns by clicking on the empty space seems to be controversial. // Resizing columns by clicking on the empty space seems to be controversial.
@@ -321,6 +322,7 @@ void BaseTreeView::mousePressEvent(QMouseEvent *ev)
void BaseTreeView::mouseReleaseEvent(QMouseEvent *ev) void BaseTreeView::mouseReleaseEvent(QMouseEvent *ev)
{ {
ItemViewEvent ive(ev, this); ItemViewEvent ive(ev, this);
QTC_ASSERT(model(), return);
if (!model()->setData(ive.index(), QVariant::fromValue(ive), ItemViewEventRole)) if (!model()->setData(ive.index(), QVariant::fromValue(ive), ItemViewEventRole))
TreeView::mouseReleaseEvent(ev); TreeView::mouseReleaseEvent(ev);
} }
@@ -444,15 +446,17 @@ ItemViewEvent::ItemViewEvent(QEvent *ev, QAbstractItemView *view)
m_index = view->indexAt(m_pos); m_index = view->indexAt(m_pos);
break; break;
default: default:
m_index = selection->currentIndex(); m_index = selection ? selection->currentIndex() : QModelIndex();
break; break;
} }
m_selectedRows = selection->selectedRows(); if (selection) {
if (m_selectedRows.isEmpty()) { m_selectedRows = selection->selectedRows();
QModelIndex current = selection->currentIndex(); if (m_selectedRows.isEmpty()) {
if (current.isValid()) QModelIndex current = selection->currentIndex();
m_selectedRows.append(current); if (current.isValid())
m_selectedRows.append(current);
}
} }
} }