diff options
author | Arno <am@disconnect.de> | 2012-10-03 14:06:11 +0200 |
---|---|---|
committer | Arno <am@disconnect.de> | 2012-10-03 14:06:11 +0200 |
commit | df88caf7df0f3b43c5498c09466a37670b764b9a (patch) | |
tree | 4e7f2b84ed0321821ad21e4782857c4c8165fac5 | |
parent | 887c2cd696c54af0cd6fdd54950d006626afeacc (diff) | |
download | SheMov-df88caf7df0f3b43c5498c09466a37670b764b9a.tar.gz SheMov-df88caf7df0f3b43c5498c09466a37670b764b9a.tar.bz2 SheMov-df88caf7df0f3b43c5498c09466a37670b764b9a.zip |
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)!
-rw-r--r-- | smtreemodel.cpp | 32 |
1 files changed, 15 insertions, 17 deletions
diff --git a/smtreemodel.cpp b/smtreemodel.cpp index 98fa4c8..088d393 100644 --- a/smtreemodel.cpp +++ b/smtreemodel.cpp @@ -9,8 +9,6 @@ #include <QSettings> #include <QBrush> -#include <QDebug> - #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<SmTreeItem*>(parent.internalPointer()); } - SmTreeItem *parentItem = static_cast<SmTreeItem*>(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<SmTreeItem*>(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{ |