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. Expanding some branches of a treeview

Expanding some branches of a treeview

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 3.0k Views 1 Watching
  • 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.
  • T Offline
    T Offline
    tony67
    wrote on last edited by tony67
    #1

    Hi All,
    in my code I produce a then push it onto a tree view as below

    //your code here
    
    header
    
        QStandardItemModel tree_model;
    
    header
    
    //your code here
    
    source
    
        QStandardItem* item;
        for( unsigned int i = 0; i < score.win_element_list.size( ); i++ )
        {
    
    /*
            if( ( int ) score.win_element_list.at( i ).data_a > current_level )
            {
                 current_level++;
                 sub_level = 0;
                 level_number = QString::number( current_level );
    
                    QString level = "level " + level_number;
                    item = new QStandardItem( QString( level ) );
    
    
                    QString wording = "Element\t\t";
                    wording += QString::number( sub_level );
                    QStandardItem* function = new QStandardItem( QString( wording ));
                    item->appendRow( function );
    
    
    
    ........
    then at end I use
    
    tree_model.setItem( column, 0, item);
    
    

    which produces the tree as needed.
    However when I call this draw function ( after I've added new elements ect ) I lose all the expanded branches of my tree. However I've stored if a branch is expanded as a variable. Is there some way I can step through treeview or QStandardItemModel. I've check the documents and can see QTreeView::setExpanded but I can't work out how to get QModelIndex from treeview or QStandardItemModel. Can anyone advise please? Thanks

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

      Hi
      Im not 100% sure what question is, but you can use index (in the model) function to get QModelIndex.
      This function will expand a parent and all sub.childs.

      void MainWindow::expandNode(const QModelIndex &parentIndex, bool expand) {
        tree->setExpanded(parentIndex, expand);
        for (qint32 rowNum = 0; rowNum < treeModel->rowCount(parentIndex); ++rowNum) {
          QModelIndex childIndex = treeModel->index(rowNum, 0, parentIndex);
          tree->setExpanded(childIndex, expand);
          expandNode(childIndex);
        }
      }
      
      1 Reply Last reply
      2

      • Login

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