From 473410b99b362d0f39017d3390d5c44e513b8b8c Mon Sep 17 00:00:00 2001 From: Gabriel de Dietrich Date: Thu, 21 May 2015 13:44:38 +0200 Subject: TreeView: Check that indexes belong to the current model And warn the user about it. We omit printing anything else in case we would be holding an invalid reference to some model. Internally, we assert if, despite the guards, we still get the wrong model indexes. Change-Id: I8e7451375d19c2aa406257a9b0193d59f69b0355 Task-number: QTBUG-46214 Reviewed-by: Caroline Chao Reviewed-by: J-P Nurmi --- src/controls/Private/qquicktreemodeladaptor.cpp | 3 +++ src/controls/TreeView.qml | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/controls/Private/qquicktreemodeladaptor.cpp b/src/controls/Private/qquicktreemodeladaptor.cpp index 6bfa737d..c9e31712 100644 --- a/src/controls/Private/qquicktreemodeladaptor.cpp +++ b/src/controls/Private/qquicktreemodeladaptor.cpp @@ -354,6 +354,7 @@ void QQuickTreeModelAdaptor::expand(const QModelIndex &idx) ASSERT_CONSISTENCY(); if (!m_model) return; + Q_ASSERT(!idx.isValid() || idx.model() == m_model); if (!idx.isValid() || !m_model->hasChildren(idx)) return; if (m_expandedItems.contains(idx)) @@ -374,6 +375,7 @@ void QQuickTreeModelAdaptor::collapse(const QModelIndex &idx) ASSERT_CONSISTENCY(); if (!m_model) return; + Q_ASSERT(!idx.isValid() || idx.model() == m_model); if (!idx.isValid() || !m_model->hasChildren(idx)) return; if (!m_expandedItems.contains(idx)) @@ -394,6 +396,7 @@ bool QQuickTreeModelAdaptor::isExpanded(const QModelIndex &index) const ASSERT_CONSISTENCY(); if (!m_model) return false; + Q_ASSERT(!index.isValid() || index.model() == m_model); return !index.isValid() || m_expandedItems.contains(index); } diff --git a/src/controls/TreeView.qml b/src/controls/TreeView.qml index 08f0da4d..84c30edd 100644 --- a/src/controls/TreeView.qml +++ b/src/controls/TreeView.qml @@ -56,15 +56,25 @@ BasicTableView { signal collapsed(var index) function isExpanded(index) { + if (index.valid && index.model !== model) { + console.warn("TreeView.isExpanded: model and index mismatch") + return false + } return modelAdaptor.isExpanded(index) } function collapse(index) { - modelAdaptor.collapse(index) + if (index.valid && index.model !== model) + console.warn("TreeView.collapse: model and index mismatch") + else + modelAdaptor.collapse(index) } function expand(index) { - modelAdaptor.expand(index) + if (index.valid && index.model !== model) + console.warn("TreeView.expand: model and index mismatch") + else + modelAdaptor.expand(index) } function indexAt(x, y) { -- cgit v1.2.1