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

Select item in QTreeWidget

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 658 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.
  • D Offline
    D Offline
    Dan203
    wrote on last edited by
    #1

    I'm trying to select an item in a QTreeWidget via code and it's not working. If I call setCurrentItem with the item I want to select it seems to change it internally, but it doesn't visually change the selection in the control. For example I can still see the previous item selected, but if I press the down arrow on the keyboard the item below the one I set in the code is suddenly highlighted. Is there some way to force the control to redraw or another function I can call that updates the visual selection? I tried calling update() but that didn't work.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      Dan203
      wrote on last edited by Dan203
      #2

      Figured it out. What I was trying to do was make it so if you selected a top level item in the tree it would select it's first child instead. But I was doing this from the currentItemChanged slot, which apparently doesn't work. So instead I had to use a QTimer::singleShot to call it after the signal was done...

      void on_treeWidget_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
      {
      	if (current->childCount())
      	{
      		QTimer::singleShot(0, ui->treeWidget, [this, current](){
      			ui->treeWidget->setCurrentItem(current->child(0));
      		});
      	}
      }
      
      
      1 Reply Last reply
      1

      • Login

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