[solved] Get Standard Item from Index returns NULL for QTreeView
-
wrote on 22 Nov 2012, 09:32 last edited by
Hi, I have small project, which has QTreeView with QStandardItemModel.
Requirement is, when user click tree parent item, I want to know total number of children in that parent item. Below is the code I have written, [which is not working as expected]@void MainWindow::on_treeView_clicked(const QModelIndex &index)
{
QStandardItem *myitm = myItemModel->itemFromIndex(ui->treeView->selectionModel()->currentIndex());
if(myitm) //this is returning NULL always
{
qDebug()<<"Child Count = " << myitm->rowCount();
}
}@Code does not work, it compiles without error, but during run-time itemFromIndex() returns NULL and hence row count doesn't execute.
Please help me solving this out. -
wrote on 22 Nov 2012, 10:50 last edited by
You can direcly get the index from the parameter's itself
@void MainWindow::on_treeView_clicked(const QModelIndex &index)
{
QStandardItem *myitm = myItemModel->itemFromIndex(index);
if(myitm) //check now
qDebug()<<"Child Count = " << myitm->rowCount();
}@Written brain to terminal : works
-
wrote on 22 Nov 2012, 11:03 last edited by
clicked might be emitted, before the selectionModel is updated, so the index of the selectionModel might be wrong.
But the index of the item that was clicked is part of the signal, so use that one, or connect to a selectionModel signal, then the selection model will be updated before. -
wrote on 22 Nov 2012, 11:10 last edited by
Thats strange , I actually checked with the following code which is the same as posted in the first post and it works
@void MyTestClass::onTreeViewClicked(const QModelIndex &index)
{
QStandardItem *item = treeModel->itemFromIndex(m_treeView->selectionModel()->currentIndex());
if (item)
qDebug() << item->text();
}@I connect using :
@connect(m_treeView,SIGNAL(clicked(QModelIndex)),SLOT(onTreeViewClicked(QModelIndex)));@
so why it worked for me n not for the first post?
-
wrote on 22 Nov 2012, 11:18 last edited by
Maybe you have a different selection mode?
Or a pre selected item? -
wrote on 22 Nov 2012, 11:27 last edited by
But I didnt explicitly set any selection mode, I have the default one (QAbstractItemView::SingleSelection), Also the item is selected when I click on the treeView Node.
Thanks for the help :)
-
wrote on 22 Nov 2012, 11:42 last edited by
Hi sam, thanks for the reply. Yes, code you have modified works fine !!.
But, I forgot to mention initially,
itemFromIndex() api I am not calling inside the click event, but inside another custom defined function, as like,
@int GetItemLevel(QTreeView *tree)
{
...
QStandardItem *myitm = myItemModel->itemFromIndex(tree->selectionModel()->currentIndex());
}
@and inside click event, I am calling this,
@int level = GetItemLevel(ui->treeView);@
I was accessing ModelIndex from treeView() and again Item from that model index. Which did not workout.
But yes, code you have given works best, no doubt.
So using your solution, I found temporary workaround, I just passed "index" parameter to@GetItemLevel(QTreeView *tree, QModelIndex *CurIndex)@
solved the problem. I don't know how far this solution will going to help me.
Sam, if you know the reason why my code returns NULL, let me know please. Again, thanks for your help. -
wrote on 22 Nov 2012, 11:50 last edited by
Gerolf, thanks for your information.
bq. clicked might be emitted, before the selectionModel is updated, so the index of the selectionModel might be wrong.
this might be the reason probably.
Thanks all for your help. -
wrote on 22 Nov 2012, 11:52 last edited by
Yes I too agree with Gerolf's explanation . I was just curious to reproduce the same for my testApp.
Anyhow since its working you can edit your first post and prepend [Solved] to the title.Happy Coding!!!
Regards
Soumitra.
1/9