
Originally Posted by
wysota
This example shows that the view does not query the whole model when it initializes:

Originally Posted by
wysota
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 :
{
if ( ! hasIndex(row, column, parent) )
TreeItem *parentItem;
if ( ! parent.isValid() )
parentItem = m_rootItem;
else
parentItem = static_cast<TreeItem*>( parent.internalPointer() );
TreeItem *childItem = parentItem->child( row );
if ( childItem )
return createIndex( row, column, childItem );
else
}
{
if ( ! index.isValid() )
TreeItem *childItem = static_cast<TreeItem*>( index.internalPointer() );
TreeItem *parentItem = childItem->parent();
if ( (parentItem == m_rootItem) || (parentItem == NULL))
return createIndex( parentItem->row(), 0, parentItem );
}
int TreeModel
::rowCount( const QModelIndex &parent
) const {
if ( parent.column() > 0 )
return 0;
TreeItem *parentItem;
if ( ! parent.isValid() )
parentItem = m_rootItem;
else
parentItem = static_cast<TreeItem*>( parent.internalPointer() );
return parentItem->childCount();
}
QModelIndex TreeModel::index( int row, int column, const QModelIndex &parent ) const
{
if ( ! hasIndex(row, column, parent) )
return QModelIndex();
TreeItem *parentItem;
if ( ! parent.isValid() )
parentItem = m_rootItem;
else
parentItem = static_cast<TreeItem*>( parent.internalPointer() );
TreeItem *childItem = parentItem->child( row );
if ( childItem )
return createIndex( row, column, childItem );
else
return QModelIndex();
}
QModelIndex TreeModel::parent( const QModelIndex &index ) const
{
if ( ! index.isValid() )
return QModelIndex();
TreeItem *childItem = static_cast<TreeItem*>( index.internalPointer() );
TreeItem *parentItem = childItem->parent();
if ( (parentItem == m_rootItem) || (parentItem == NULL))
return QModelIndex();
return createIndex( parentItem->row(), 0, parentItem );
}
int TreeModel::rowCount( const QModelIndex &parent ) const
{
if ( parent.column() > 0 )
return 0;
TreeItem *parentItem;
if ( ! parent.isValid() )
parentItem = m_rootItem;
else
parentItem = static_cast<TreeItem*>( parent.internalPointer() );
return parentItem->childCount();
}
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.
qDebug() << Q_FUNC_INFO << parent;
if(parent.isValid()) return 2; //each parent with 2 child
return 10; // 10 top most parrents
}
if(parent.isValid()){ // this condition is not at all executing
qDebug() << "Yes isValid = " << parent.child(row, column);
return createIndex(row, column, (void*)parent.child(row, column).internalPointer()); //create &return child index
}
return createIndex(row, column, (void*)0); // return top most elements index's
}
int rowCount(const QModelIndex &parent = QModelIndex()) const {
qDebug() << Q_FUNC_INFO << parent;
if(parent.isValid()) return 2; //each parent with 2 child
return 10; // 10 top most parrents
}
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const {
if(parent.isValid()){ // this condition is not at all executing
qDebug() << "Yes isValid = " << parent.child(row, column);
return createIndex(row, column, (void*)parent.child(row, column).internalPointer()); //create &return child index
}
return createIndex(row, column, (void*)0); // return top most elements index's
}
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).
Bookmarks