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. QTreeView not refresh correctly

QTreeView not refresh correctly

Scheduled Pinned Locked Moved Solved General and Desktop
qabstractitemmoqtreeviewitemmodel
3 Posts 2 Posters 3.0k Views
  • 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.
  • N Offline
    N Offline
    NicolasS
    wrote on last edited by
    #1

    Hi all,

    In my application, I have a tree hierarchical structure FooTree that contains Foo* objects.
    Each Foo object knows how many children it has and also its index in its parent's children table.

    Now I'd like to display the FooTree in a QTreeView.
    I've reimplemented my own QAbstractItemModel where each QModelIndex owns a Foo* (based on doc from http://doc.qt.io/qt-5/modelview.html)
    For example, rowCount() and index() looks like this :

    int FooModel::rowCount(QModelIndex & parent) int
    {
    	return static_cast<Foo*>(parent.internalPointer())->GetNumberOfChild();
    }
    
    QModelIndex FooModel::index(int row,int column,const QModelIndex &parent) const
    {
    	Foo* FooParent=NULL;
    	if(!parent.isValid())
    		FooParent = m_Root;
    	else
    		FooParent = static_cast<Foo*>(parent.internalPointer());
    
    	Foo* FooChild = NULL;
    	if(FooParent )
    	{
    		int nbChildren = FooParent->GetChildrenNumber();
    		if( row >= 0 && row < nbChildren)
    			FooChild = FooParent->GetChild(row);
    	}
    
    	if(FooChild)
    		indexToReturn=createIndex( row, column, FooChild);
    
    	return indexToReturn;
    }
    

    When I launch my QTreeView, it's ok, the view displays the existing FooTree correctly.

    My problem is when I append Foo* Child to an existing Foo* Parent in my FooTree, I can't have the view to refresh correctly.

    For the moment, once the Foo* has been added I call this method on the model :

    void FooModel::FooHasBeenAdded( Foo* foo)
    {
    	QModelIndex parentIndex = IndexOf( foo->GetParent());	// Custom func to retrieve the index of a Foo*
    	emit dataChanged( parentIndex, parentIndex);
    }
    

    I would expect that emitting dataChanged(parentIndex, parentIndex) would re-create all the indices under parentIndex (because rowCount(parentIndex) has changed) and then notify the view but it doesn't work.

    Any ideas ? Is there something wrong with my code ?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      You should also call the appropriate beginInsertRows and endInsertRows when you add data to your structure. That will trigger the proper update for the views

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      N 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        You should also call the appropriate beginInsertRows and endInsertRows when you add data to your structure. That will trigger the proper update for the views

        N Offline
        N Offline
        NicolasS
        wrote on last edited by
        #3

        @SGaist Thanks for your advise, it works like well now.

        1 Reply Last reply
        0

        • Login

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