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. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

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

    Quote Originally Posted by prasad_N View Post
    these 2 statements are conflicting right ?
    Yes, I didn't express myself clearly. I meant that it is the view which is causing what you see, not the model. I didn't mean to say that "the view has to query all the indexes".

    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 
    This is the implementation of QAbstractItemModel::hasIndex():
    Qt Code:
    1. bool QAbstractItemModel::hasIndex(int row, int column, const QModelIndex &parent) const
    2. {
    3. if (row < 0 || column < 0)
    4. return false;
    5. return row < rowCount(parent) && column < columnCount(parent);
    6. }
    To copy to clipboard, switch view to plain text mode 

    Therefore each time you call you index() implementation, it calls hasIndex() which in turn calls rowCount() for the parent. Possibly this applies to other methods and their default implementations.

    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 ?
    Most likely you didn't change the base class to QAbstractItemModel. Apparently QTreeView has an optimization to not check child nodes for flat models.

    Qt Code:
    1. #include <QtWidgets>
    2.  
    3. #define BASEMODEL QAbstractItemModel
    4.  
    5. class Model : public BASEMODEL {
    6. public:
    7. Model(QObject *parent = 0) : BASEMODEL(parent) {}
    8.  
    9. int columnCount(const QModelIndex &parent = QModelIndex()) const { return 1; }
    10.  
    11. int rowCount(const QModelIndex &parent = QModelIndex()) const {
    12. qDebug() << Q_FUNC_INFO << parent;
    13. if(!parent.isValid()) return 10;
    14. if(parent.isValid() && !parent.parent().isValid()) return 2; // limit to two levels
    15.  
    16. return 0;
    17. }
    18. QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const {
    19. qDebug() << Q_FUNC_INFO << row << column << parent;
    20. if(parent.isValid()) {
    21. qDebug() << Q_FUNC_INFO << "parent valid";
    22. // we only need id of the parent since it identifies the item
    23. return createIndex(row, column, parent.row());
    24. }
    25. return createIndex(row, column, -1); // top level item has invalid parent
    26. }
    27.  
    28. QModelIndex parent(const QModelIndex &ind) const {
    29. qDebug() << Q_FUNC_INFO << ind;
    30. if(!ind.isValid()) return QModelIndex();
    31. int id = ind.internalId();
    32. if( id == -1) return QModelIndex();
    33. return index(id, 0); // return top-level item
    34. }
    35.  
    36. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const {
    37. if(role == Qt::DisplayRole) return index.row()+1;
    38. return QVariant();
    39. }
    40. };
    41.  
    42. int main(int argc, char **argv) {
    43. QApplication app(argc, argv);
    44. Model model;
    45. v.setModel(&model);
    46. v.show();
    47. return app.exec();
    48. }
    To copy to clipboard, switch view to plain text mode 

    I don't want to use treeItems in my model for tree(if possible, I think it is possible with above implementation).
    Then don't use them

    By the way, changing the base class to QAbstractItemModel does cause the tree view to query rowCount for all the top-level items at start, I will try to see why this happens. You can probably switch some property and it will stop doing that.

    Edit: It looks like during layout of the tree, the widget calls rowCount() on items that have children. What is worrying is that it also calls canFetchMore() && fetchMore() for them which in my opinion defeats the purpose of implementing those two methods at all at least for two level models.
    Last edited by wysota; 8th July 2015 at 20:26.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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.