Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Auto select child in QTreeView
Qt 6.12 LTS is out! See what's new in the release blog

[SOLVED] Auto select child in QTreeView

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 8.5k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    I'm using QTreeView (model) to show some categories for user to select. Let say I have this tree view :

    @Linux
    |__ Ubuntu
    |__ Fedora
    |__ OpenSUSE@

    How can the tree view will select all the child under Linux when I click it?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sam
      wrote on last edited by
      #2

      First you need to connect signal (clicked()) for your treeView :-

      @connect(m_treeView,SIGNAL(clicked(QModelIndex)),SLOT(onTreeViewClicked(QModelIndex)));@

      and in the implementation code you can write:

      @void YourClass::onTreeViewClicked(const QModelIndex &index)
      {
      QStandardItem *item = treeModel->itemFromIndex(index);

      for (int r=0; r<item->rowCount(); r++)
      {
          QModelIndex childIndex = treeModel->index(r,0,index);
          m_treeView->selectionModel()->select(childIndex,QItemSelectionModel::Select);
      }
      

      }@

      You also need to set the selection mode for your treeView
      @m_treeView->setSelectionMode(QAbstractItemView::MultiSelection);@

      You can change/clear the selection as per your requirement.

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        I know it's late...thank you!

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Sam
          wrote on last edited by
          #4

          You are welcome :)

          If the issue is solved , Kindly edit your first post and add [SOLVED] to the title.

          Regards
          Soumitra.

          1 Reply Last reply
          0
          • X Offline
            X Offline
            XGuy
            wrote on last edited by
            #5

            i'm using this code and it perfectly works, but also i want to give the user the ability to deselect each one he wished! any ideas??

            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
            • Unsolved