Results 1 to 20 of 23

Thread: rowCount() is calling for all the top level items

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #5
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: rowCount() is calling for all the top level items

    Quote Originally Posted by wysota View Post
    This example shows that the view does not query the whole model when it initializes:
    Quote Originally Posted by wysota View Post
    No, it's a default behaviour of QTreeView since the tree wants to know whether each of the items has child items:
    these 2 statements are conflicting right ?

    1. In my model rowCount is calling for all the top level items.

    my model is :

    Qt Code:
    1. QModelIndex TreeModel::index( int row, int column, const QModelIndex &parent ) const
    2. {
    3. if ( ! hasIndex(row, column, parent) )
    4. return QModelIndex();
    5.  
    6. TreeItem *parentItem;
    7.  
    8. if ( ! parent.isValid() )
    9. parentItem = m_rootItem;
    10. else
    11. parentItem = static_cast<TreeItem*>( parent.internalPointer() );
    12.  
    13. TreeItem *childItem = parentItem->child( row );
    14. if ( childItem )
    15. return createIndex( row, column, childItem );
    16. else
    17. return QModelIndex();
    18. }
    19.  
    20. QModelIndex TreeModel::parent( const QModelIndex &index ) const
    21. {
    22. if ( ! index.isValid() )
    23. return QModelIndex();
    24.  
    25. TreeItem *childItem = static_cast<TreeItem*>( index.internalPointer() );
    26. TreeItem *parentItem = childItem->parent();
    27.  
    28. if ( (parentItem == m_rootItem) || (parentItem == NULL))
    29. return QModelIndex();
    30.  
    31. return createIndex( parentItem->row(), 0, parentItem );
    32. }
    33.  
    34. int TreeModel::rowCount( const QModelIndex &parent ) const
    35. {
    36. if ( parent.column() > 0 )
    37. return 0;
    38.  
    39. TreeItem *parentItem;
    40. if ( ! parent.isValid() )
    41. parentItem = m_rootItem;
    42. else
    43. parentItem = static_cast<TreeItem*>( parent.internalPointer() );
    44.  
    45. return parentItem->childCount();
    46. }
    To copy to clipboard, switch view to plain text mode 


    2. When I change your model little bit on order to check tree function calling. in the index if(parent.isValid()) is not at all executing.

    Qt Code:
    1. int rowCount(const QModelIndex &parent = QModelIndex()) const {
    2. qDebug() << Q_FUNC_INFO << parent;
    3. if(parent.isValid()) return 2; //each parent with 2 child
    4. return 10; // 10 top most parrents
    5. }
    6.  
    7. QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const {
    8. if(parent.isValid()){ // this condition is not at all executing
    9. qDebug() << "Yes isValid = " << parent.child(row, column);
    10. return createIndex(row, column, (void*)parent.child(row, column).internalPointer()); //create &return child index
    11. }
    12.  
    13. return createIndex(row, column, (void*)0); // return top most elements index's
    14. }
    To copy to clipboard, switch view to plain text mode 


    What wrong am I doing here ?

    I don't want to use treeItems in my model for tree(if possible, I think it is possible with above implementation).
    Last edited by prasad_N; 8th July 2015 at 19:35.
    Thanks :-)

Similar Threads

  1. Replies: 10
    Last Post: 21st May 2015, 18:11
  2. Replies: 16
    Last Post: 12th May 2015, 21:46
  3. Replies: 1
    Last Post: 14th March 2013, 22:50
  4. QTreeWidget::findItems only searches top level items ?
    By krisha in forum Qt Programming
    Replies: 3
    Last Post: 27th October 2011, 08:28
  5. Replies: 4
    Last Post: 4th June 2007, 12:07

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.