Select item in QTreeWidget
-
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.
-
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)); }); } }