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)
{
ItemViewEvent ive(ev, this);
QTC_ASSERT(model(), return);
if (!model()->setData(ive.index(), QVariant::fromValue(ive), ItemViewEventRole))
TreeView::mousePressEvent(ev);
// 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)
{
ItemViewEvent ive(ev, this);
QTC_ASSERT(model(), return);
if (!model()->setData(ive.index(), QVariant::fromValue(ive), ItemViewEventRole))
TreeView::mouseReleaseEvent(ev);
}
@@ -444,10 +446,11 @@ ItemViewEvent::ItemViewEvent(QEvent *ev, QAbstractItemView *view)
m_index = view->indexAt(m_pos);
break;
default:
m_index = selection->currentIndex();
m_index = selection ? selection->currentIndex() : QModelIndex();
break;
}
if (selection) {
m_selectedRows = selection->selectedRows();
if (m_selectedRows.isEmpty()) {
QModelIndex current = selection->currentIndex();
@@ -455,6 +458,7 @@ ItemViewEvent::ItemViewEvent(QEvent *ev, QAbstractItemView *view)
m_selectedRows.append(current);
}
}
}
QModelIndexList ItemViewEvent::currentOrSelectedRows() const
{