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. Automatically select children when parent is selected in treeview

Automatically select children when parent is selected in treeview

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 887 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.
  • I Offline
    I Offline
    IknowQT
    wrote on 8 Apr 2022, 05:13 last edited by
    #1

    The index is the parent, and the children below it are well selected.
    But after all the children are selected, the parent becomes unselected.

    Is there a way to get both parent and child selected?

    void wMethodInfo::SelectModelIndex(const QModelIndex index)
    {
    	//*item = m_pDirModel->itemFromIndex(index);
    
    	if (this->ui.treeView->selectionMode() == QAbstractItemView::SingleSelection)
    		return;
    
    	this->ui.treeView->setSelectionMode(QAbstractItemView::MultiSelection);
    	this->ui.treeView->selectionModel()->select(index, QItemSelectionModel::Select);
    
    	for (int i = 0; i < m_pDirModel->rowCount(index); i++)
    	{
    		QModelIndex child = m_pDirModel->index(i, 0, index);
    		this->ui.treeView->selectionModel()->select(child, QItemSelectionModel::Select);
    	}
    }
    
    I J V 3 Replies Last reply 8 Apr 2022, 05:15
    0
    • I IknowQT
      8 Apr 2022, 05:13

      The index is the parent, and the children below it are well selected.
      But after all the children are selected, the parent becomes unselected.

      Is there a way to get both parent and child selected?

      void wMethodInfo::SelectModelIndex(const QModelIndex index)
      {
      	//*item = m_pDirModel->itemFromIndex(index);
      
      	if (this->ui.treeView->selectionMode() == QAbstractItemView::SingleSelection)
      		return;
      
      	this->ui.treeView->setSelectionMode(QAbstractItemView::MultiSelection);
      	this->ui.treeView->selectionModel()->select(index, QItemSelectionModel::Select);
      
      	for (int i = 0; i < m_pDirModel->rowCount(index); i++)
      	{
      		QModelIndex child = m_pDirModel->index(i, 0, index);
      		this->ui.treeView->selectionModel()->select(child, QItemSelectionModel::Select);
      	}
      }
      
      I Offline
      I Offline
      IknowQT
      wrote on 8 Apr 2022, 05:15 last edited by
      #2

      @IknowQT

      Additional questions I would like to delete after multiple selection.
      How can I do it?

      1 Reply Last reply
      0
      • I IknowQT
        8 Apr 2022, 05:13

        The index is the parent, and the children below it are well selected.
        But after all the children are selected, the parent becomes unselected.

        Is there a way to get both parent and child selected?

        void wMethodInfo::SelectModelIndex(const QModelIndex index)
        {
        	//*item = m_pDirModel->itemFromIndex(index);
        
        	if (this->ui.treeView->selectionMode() == QAbstractItemView::SingleSelection)
        		return;
        
        	this->ui.treeView->setSelectionMode(QAbstractItemView::MultiSelection);
        	this->ui.treeView->selectionModel()->select(index, QItemSelectionModel::Select);
        
        	for (int i = 0; i < m_pDirModel->rowCount(index); i++)
        	{
        		QModelIndex child = m_pDirModel->index(i, 0, index);
        		this->ui.treeView->selectionModel()->select(child, QItemSelectionModel::Select);
        	}
        }
        
        J Offline
        J Offline
        JonB
        wrote on 8 Apr 2022, 07:28 last edited by
        #3

        @IknowQT said in Automatically select children when parent is selected in treeview:

        But after all the children are selected, the parent becomes unselected.

        I am "surprised" at this behaviour, but have not tested. Are you saying that as soon as you just go

        this->ui.treeView->selectionModel()->select(m_pDirModel->index(0, 0, index), QItemSelectionModel::Select);
        

        to select the first child the parent automatically becomes unselected? Maybe QTreeView does not allow both parent nodes and child leaves to be selected at the same time, though not sure why that would be.

        Additional questions I would like to delete after multiple selection.

        So when Delete pressed go through all selected nodes/leaves deleting them. However note in this case it doesn't make much sense to allow children of a selected node to be selected or unselected: either way, if you are going to delete a parent node all its children will get deleted regardless.

        1 Reply Last reply
        0
        • I IknowQT
          8 Apr 2022, 05:13

          The index is the parent, and the children below it are well selected.
          But after all the children are selected, the parent becomes unselected.

          Is there a way to get both parent and child selected?

          void wMethodInfo::SelectModelIndex(const QModelIndex index)
          {
          	//*item = m_pDirModel->itemFromIndex(index);
          
          	if (this->ui.treeView->selectionMode() == QAbstractItemView::SingleSelection)
          		return;
          
          	this->ui.treeView->setSelectionMode(QAbstractItemView::MultiSelection);
          	this->ui.treeView->selectionModel()->select(index, QItemSelectionModel::Select);
          
          	for (int i = 0; i < m_pDirModel->rowCount(index); i++)
          	{
          		QModelIndex child = m_pDirModel->index(i, 0, index);
          		this->ui.treeView->selectionModel()->select(child, QItemSelectionModel::Select);
          	}
          }
          
          V Offline
          V Offline
          VRonin
          wrote on 8 Apr 2022, 10:19 last edited by
          #4

          Try replacing

          this->ui.treeView->selectionModel()->select(index, QItemSelectionModel::Select);
          
          	for (int i = 0; i < m_pDirModel->rowCount(index); i++)
          	{
          		QModelIndex child = m_pDirModel->index(i, 0, index);
          		this->ui.treeView->selectionModel()->select(child, QItemSelectionModel::Select);
          	}
          

          with

          QItemSelection selection(m_pDirModel->index(0, 0, index),m_pDirModel->index(m_pDirModel->rowCount(index)-1, 0, index));
          selection.select(index,index);
          ui.treeView->selectionModel()->select(selection, QItemSelectionModel::Select);
          

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          0

          1/4

          8 Apr 2022, 05:13

          • Login

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