Need some helping making sense of QAbstractItemModel



  • I am trying to figure out how to use QAbstractItemModel in a QTreeView control and it seems I really can't figure it out. As an very simple example I am trying to build a tree with 3 items - a root with 2 children. Lets say the root has ID 100, the first child ID 1000 and the second child ID 2000.

    100
       1000
       2000
    

    My code is like this:

    int TreeData::rowCount( const QModelIndex & parent ) const
    {
    	if ( !parent.isValid( ) ) return 1;   	// asking for root items? we have 1
    	if ( parent.column( ) != 0 ) return 0;	// only column 0 can have children?
    	if ( parent.row( ) == 100 ) return 2;	// root item has ID 100 and 2 children
    	return 0;								// no other item has any children
    }
    
    int TreeData::columnCount( const QModelIndex & parent ) const
    {
    	return 1; // lets assume 1 column
    }
    
    QModelIndex TreeData::index( int row , int col , const QModelIndex & parent ) const
    {
    	qDebug() << QString::asprintf( "index(%d,%d,(%d,%d))" , row , col , parent.row( ) , parent.column( ) );
    	if ( !parent.isValid( ) )		// asking for root items?
    	{ 
    		if ( row == 0 ) return createIndex( 100 , col ); // only row 0 should be valid since rowCount( ) returned 1 ?
    	}
    	if ( parent.row( ) == 100 ) 	// asking for children of our root item?
    	{
    		if ( row == 0 ) return createIndex( 1000 , col );	// asking for child 1 ?
    		if ( row == 1 ) return createIndex( 2000 , col ); 	// asking for child 2 ?
    	}
    	return QModelIndex( );
    }
    
    QModelIndex TreeData::parent( const QModelIndex & index ) const
    {
    	if ( !index.isValid( ) ) return QModelIndex( );		// is this even needed?
    	if ( index.column( ) != 0 ) return QModelIndex( );	// is this even needed?
    	if ( index.row( ) == 100 ) return QModelIndex( );	// item 100 = no parent
    	if ( index.row( ) == 1000 || index.row( ) == 2000 ) 
    		return createIndex( 100 , 0 );					// items 1000 and 2000 = item 100 is the parent
    	return QModelIndex( );
    }
    
    QVariant TreeData::data( const QModelIndex & index , int role ) const
    {
    	if ( role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::ToolTipRole )
    		return QString::asprintf( "Item %d x %d" , index.row( ) , index.column( ) );
    	return QVariant( );
    }
    

    And the result is nothing like I expect it to be. Actually not a single item is visible on the tree. And in the log I see calls like:

    index(0,0,(-1,-1))
    index(100,0,(-1,-1))
    

    I can't understand why are those calls been made. What exactly is row parameter, I assume it's the index of the child for the given parent QModelIndex( ), but then why it gets called with value of 100? And on the other way round, if the parameter is the QModelIndex() row value why it get's called with value of 0?

    What should I modify in the code to get the result I expect?

    Thanks!


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You should take a look at the Simple Tree Model Example. It contains a pretty nice explanation on how to write a tree model.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.