From ca9cdb39a8b3eafa8106fed84cce013f18b0a114 Mon Sep 17 00:00:00 2001 From: Arno Date: Mon, 7 Jul 2014 06:30:04 +0200 Subject: Fix MappinTreeWidget::addChild Fix the lookup of a new child, so it will select the right child if we have more than one child with the same name. Do it by looking up the id instead of the name. --- mappingtreewidget.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/mappingtreewidget.cpp b/mappingtreewidget.cpp index c2da053..be0c5a5 100644 --- a/mappingtreewidget.cpp +++ b/mappingtreewidget.cpp @@ -135,17 +135,20 @@ void MappingTreeWidget::addChild(){ QModelIndex sel = selected(); parent = mProxy->mapToSource(sel); } + int parentId = parent.data(MappingTreeModel::MappingIdRole).toInt(); if(!mModel->addChild(value, parent)){ QString err = QString(tr("

Failed to create child! Database said:

%1

")).arg(mModel->lastError().text()); QMessageBox::critical(this, tr("Error"), err); }else{ // we repopulated the model, so all indexes are invalid! - // this will fail if we have 2 children with the same name... :( - QModelIndex newSIdx = mModel->findRecursive(value, 0, mModel->rootIndex()); - if(newSIdx.isValid()){ - QModelIndex newPIdx = mProxy->mapFromSource(newSIdx); - mTree->scrollTo(newPIdx, QAbstractItemView::EnsureVisible); - mTree->selectionModel()->setCurrentIndex(newPIdx, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Current | QItemSelectionModel::Rows); + QModelIndex newPIdx = mModel->findRecursive(parentId, MappingTreeModel::MappingId, mModel->rootIndex()); + if(newPIdx.isValid()){ + QModelIndex newSIdx = mModel->find(value, MappingTreeModel::Name, newPIdx); + if(newSIdx.isValid()){ + QModelIndex newSIdxP = mProxy->mapFromSource(newSIdx); + mTree->scrollTo(newSIdxP, QAbstractItemView::EnsureVisible); + mTree->selectionModel()->setCurrentIndex(newSIdxP, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Current | QItemSelectionModel::Rows); + } } } } -- cgit v1.2.3-70-g09d2