Strange behaviour of QTreeWidget (expand/collapse)



  • Dear All,

    after the migration from Qt4 to Qt5 the following code seems to work not any longer.

      //_tokenList = QTreeWidget
       QTreeWidgetItem* item( _tokensList->leafItem( pattern));
       if( item != nullptr) {
          _tokensList->collapseAll();
          _tokensList->setCurrentItem( item);
          return;
       }
    

    After selecting a token the Tree should completely collaps and with setCurrentItem re-expanded to the node. Sometimes this is working fine but often the tree remains collapsed.
    Does anybody have an idea why this happen?

    Could it be possible that there are some problems with compressed postEvents?


  • Moderators

    @markcurl Shouldn't this

    QTreeWidgetItem* item( _tokensList->leafItem( pattern));
    

    be

    QTreeWidgetItem* item = new QTreeWidgetItem( _tokensList->leafItem( pattern));
    

    ?



  • @jsulm

    No, _tokensList is derived from Qtreewidget and the function leafItem(pattern) returns a QtreeWidgetItem* to the item inside the tree which matchs the pattern. There is no need to call the constructor of QTreeWidgetItem.


  • Moderators

    @markcurl OK, I see. I was confused because you don't use assignment operator to assign a value to the pointer :-)

    QTreeWidgetItem* item = _tokensList->leafItem( pattern);
    


  • I have found the problem. The QTreeWidget expands to the item only, if the new currentIndex is presented in another node as the old currentIndex.
    Solution: reset currentIndex

    
     UNSOLVED Strange behaviour of QTreeWidget (expand/collapse)
     markcurl   Bookmark 0    0 Reply 
    
    Dear All,
    
    after the migration from Qt4 to Qt5 the following code seems to work not any longer.
    
      //_tokenList = QTreeWidget
       QTreeWidgetItem* item( _tokensList->leafItem( pattern));
       if( item != nullptr) {
          _tokensList->setCurrentItem( nullptr);
          _tokensList->collapseAll();
          _tokensList->setCurrentItem( item);
          return;
       }
    

Log in to reply
 

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