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 item expansion
Forum Updated to NodeBB v4.3 + New Features

QTreeView item expansion

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 4.8k 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.
  • A Offline
    A Offline
    aatwo
    wrote on last edited by
    #1

    Hey guys. Basically I have a QTreeView, a QSortFilterProxyModel and an implementation of a QAbstractItemModel. The model is simple, there is a root item, then at most two further levels of items.

    For example lets say my model represents Films and their actors such that the tree might look like this:

    Lord of the rings
        Sean Bean
        Ian Holm
        Ian McKellen
        Elijah Wood
        ... etc ...
    
    Watchmen
        Billy Crudup
        Patrick Wilson
        Jeffrey Dean Morgan
        Matthew Goode
        ... etc ...
    
    ... etc ...
    

    Sometimes the model is fully rebuilt, other times times there is a user filter that can be applied which may hide or show films. I keep track of which films are expanded so when an item comes into view or the model is rebuilt then I can expand them if required.

    Now this is something I have done before with no issue, but in this particular instance for some reason expanding the nodes individually using QTreeView::setExpanded(QModelIndex, bool) does not work. the QTreeView::expandAll() function works though so I am not sure what is happening.

    I have tried the following code snippet as a test to see if I can programmatically expand all nodes individually but it does not work, even if defered using a timer by something stupid like 5 seconds (also note at this point I have confirmed that the QModelIndex points to the expected item by checking the internal data).

    for( int i = 0; i < mProxyModel->sourceModel()->rowCount(); i++ )
    {
        QModelIndex sourceIndex = mProxyModel->sourceModel()->index( i, 0 );
        QModelIndex mappedIndex = mProxyModel->mapFromSource( sourceIndex );
    
        if( !mappedIndex.isValid() )
            continue;
    
        mTreeView->setExpanded( mappedIndex, true );
    }
    

    Using the following snippet does work however:

    mTreeView->expandAll();
    

    Does anyone know of anything that could be causing this?! Or perhaps some advice on how to debug this further?

    Kind regards, Aaron.

    raven-worxR 1 Reply Last reply
    0
    • A aatwo

      Hey guys. Basically I have a QTreeView, a QSortFilterProxyModel and an implementation of a QAbstractItemModel. The model is simple, there is a root item, then at most two further levels of items.

      For example lets say my model represents Films and their actors such that the tree might look like this:

      Lord of the rings
          Sean Bean
          Ian Holm
          Ian McKellen
          Elijah Wood
          ... etc ...
      
      Watchmen
          Billy Crudup
          Patrick Wilson
          Jeffrey Dean Morgan
          Matthew Goode
          ... etc ...
      
      ... etc ...
      

      Sometimes the model is fully rebuilt, other times times there is a user filter that can be applied which may hide or show films. I keep track of which films are expanded so when an item comes into view or the model is rebuilt then I can expand them if required.

      Now this is something I have done before with no issue, but in this particular instance for some reason expanding the nodes individually using QTreeView::setExpanded(QModelIndex, bool) does not work. the QTreeView::expandAll() function works though so I am not sure what is happening.

      I have tried the following code snippet as a test to see if I can programmatically expand all nodes individually but it does not work, even if defered using a timer by something stupid like 5 seconds (also note at this point I have confirmed that the QModelIndex points to the expected item by checking the internal data).

      for( int i = 0; i < mProxyModel->sourceModel()->rowCount(); i++ )
      {
          QModelIndex sourceIndex = mProxyModel->sourceModel()->index( i, 0 );
          QModelIndex mappedIndex = mProxyModel->mapFromSource( sourceIndex );
      
          if( !mappedIndex.isValid() )
              continue;
      
          mTreeView->setExpanded( mappedIndex, true );
      }
      

      Using the following snippet does work however:

      mTreeView->expandAll();
      

      Does anyone know of anything that could be causing this?! Or perhaps some advice on how to debug this further?

      Kind regards, Aaron.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @aatwo
      when is your expanding-loop called?

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      A 1 Reply Last reply
      0
      • raven-worxR raven-worx

        @aatwo
        when is your expanding-loop called?

        A Offline
        A Offline
        aatwo
        wrote on last edited by
        #3

        @raven-worx Under normal conditions right after my model emits a signal to indicate that it has been rebuilt, and also just after the user modifies the filter text:

        void onFilterTextChanged(const QString& text)
        {
            mProxyModel->setFilterText(text);  // This function stores the filter text as a member and then calls 'QSortFilterProxyModel::invalidateFilter()'
            
            for( int i = 0; i < mProxyModel->sourceModel()->rowCount(); i++ )
            {
                QModelIndex sourceIndex = mProxyModel->sourceModel()->index( i, 0 );
                QModelIndex mappedIndex = mProxyModel->mapFromSource( sourceIndex );
            
                if( !mappedIndex.isValid() )
                    continue;
            
                mTreeView->setExpanded( mappedIndex, true );
            }
        }
        

        But even if I take that loop, and put it in a slot that gets called after 10 seconds it doesn't work. It's worth noting that my application has another similar model + proxy + view implementation, and I tried adding a slot that gets called after 10 seconds to expand all the tree items using the exact same loop as above and it works as expected. The proxy models for these two models are almost identical so my only guess at this point is that there is something odd happening in the model.

        L 1 Reply Last reply
        0
        • A aatwo

          @raven-worx Under normal conditions right after my model emits a signal to indicate that it has been rebuilt, and also just after the user modifies the filter text:

          void onFilterTextChanged(const QString& text)
          {
              mProxyModel->setFilterText(text);  // This function stores the filter text as a member and then calls 'QSortFilterProxyModel::invalidateFilter()'
              
              for( int i = 0; i < mProxyModel->sourceModel()->rowCount(); i++ )
              {
                  QModelIndex sourceIndex = mProxyModel->sourceModel()->index( i, 0 );
                  QModelIndex mappedIndex = mProxyModel->mapFromSource( sourceIndex );
              
                  if( !mappedIndex.isValid() )
                      continue;
              
                  mTreeView->setExpanded( mappedIndex, true );
              }
          }
          

          But even if I take that loop, and put it in a slot that gets called after 10 seconds it doesn't work. It's worth noting that my application has another similar model + proxy + view implementation, and I tried adding a slot that gets called after 10 seconds to expand all the tree items using the exact same loop as above and it works as expected. The proxy models for these two models are almost identical so my only guess at this point is that there is something odd happening in the model.

          L Offline
          L Offline
          learningAlways
          wrote on last edited by
          #4

          @aatwo Did you receive a solution?

          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