From df88caf7df0f3b43c5498c09466a37670b764b9a Mon Sep 17 00:00:00 2001 From: Arno Date: Wed, 3 Oct 2012 14:06:11 +0200 Subject: Fix SmTreeModel for good Back to the basics! Should have read the reference implementation of QAbstractItemModel::index() earlier, much earlier. Now it just works (tm)! --- smtreemodel.cpp | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) (limited to 'smtreemodel.cpp') diff --git a/smtreemodel.cpp b/smtreemodel.cpp index 98fa4c8..088d393 100644 --- a/smtreemodel.cpp +++ b/smtreemodel.cpp @@ -9,8 +9,6 @@ #include #include -#include - #include "smtreemodel.h" #include "smtreeitem.h" #include "smglobals.h" @@ -42,20 +40,20 @@ int SmTreeModel::columnCount(const QModelIndex &parent) const{ } QModelIndex SmTreeModel::index(int row, int column, const QModelIndex &parent) const{ - // return child from root if parent is invalid - // this was difficult to figure this out! + if(!hasIndex(row, column, parent)){ + return QModelIndex(); + } + SmTreeItem *parentItem; if(!parent.isValid()){ - return createIndex(row, column, mRootItem->child(row)); + parentItem = mRootItem; + }else{ + parentItem = static_cast(parent.internalPointer()); } - SmTreeItem *parentItem = static_cast(parent.internalPointer()); - if( (parentItem == 0) || - (row < 0) || - (column < 0) || - (row >= parentItem->childCount()) || - (column > parentItem->columnCount())){ - return QModelIndex(); + SmTreeItem *childItem = parentItem->child(row); + if(childItem){ + return createIndex(row, column, childItem); } - return createIndex(row, column, parentItem->child(row)); + return QModelIndex(); } QModelIndex SmTreeModel::parent(const QModelIndex &child) const{ @@ -66,10 +64,10 @@ QModelIndex SmTreeModel::parent(const QModelIndex &child) const{ SmTreeItem *childItem = static_cast(child.internalPointer()); SmTreeItem *parentItem = childItem->parent(); - if(parentItem == 0){ - return QModelIndex(); - } - return createIndex(parentItem->row(), child.column(), parentItem); + if(parentItem == mRootItem){ + return QModelIndex(); + } + return createIndex(parentItem->row(), 0, parentItem); } Qt::ItemFlags SmTreeModel::flags(const QModelIndex &index) const{ -- cgit v1.2.3-70-g09d2