Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved] Get Standard Item from Index returns NULL for QTreeView
Forum Updated to NodeBB v4.3 + New Features

[solved] Get Standard Item from Index returns NULL for QTreeView

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 10.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kirangps
    wrote on 22 Nov 2012, 09:32 last edited by
    #1

    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.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sam
      wrote on 22 Nov 2012, 10:50 last edited by
      #2

      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

      1 Reply Last reply
      0
      • G Offline
        G Offline
        giesbert
        wrote on 22 Nov 2012, 11:03 last edited by
        #3

        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.

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Sam
          wrote on 22 Nov 2012, 11:10 last edited by
          #4

          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?

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on 22 Nov 2012, 11:18 last edited by
            #5

            Maybe you have a different selection mode?
            Or a pre selected item?

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Sam
              wrote on 22 Nov 2012, 11:27 last edited by
              #6

              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 :)

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kirangps
                wrote on 22 Nov 2012, 11:42 last edited by
                #7

                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.

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  kirangps
                  wrote on 22 Nov 2012, 11:50 last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    Sam
                    wrote on 22 Nov 2012, 11:52 last edited by
                    #9

                    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 Reply Last reply
                    0

                    1/9

                    22 Nov 2012, 09:32

                    • Login

                    • Login or register to search.
                    1 out of 9
                    • First post
                      1/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved