Deselect *visually* at start of program
-
I have a couple of
QTreeWidgets in my UI, which I fill when I init my MainWindow (Creation Process: Construct -> Init -> Show) then I always get a ghost selection in all TWs.When I fill the data into the TWs, I surround it with:
ui->itemList->clear(); ui->itemList->clearSelection(); ui->itemList->blockSignals(true); // Fill data ui->itemList->blockSignals(false);(
itemList= one of my TWs)While
clearSelectiondoes clear the selection for the code (ui->itemTree->selectedItems().isEmpty()) it does not do that visually
The item looks selected, but isn't according to the code
bool noItemSel = ui->itemList->selectedItems().isEmpty();This variable gets used to enable/disable certain buttons which should only work if an item is selected:
ui->pbItemAdd->setDisabled(noTreeRights); ui->pbItemEdit->setDisabled(noItemSel || noTreeRights); ui->pbItemDelete->setDisabled(noItemSel || noTreeRights);these buttons, shown here

get disabled,noTreeRightsis false, as seen by the add button, so that one doesnt cause the issue of disabling. Theres a lot of other loading done when someone selects an Item, so I'd rather keep the tree widget unselected than selecting the first itemSo how do I remove that visual selection?
-
I have a couple of
QTreeWidgets in my UI, which I fill when I init my MainWindow (Creation Process: Construct -> Init -> Show) then I always get a ghost selection in all TWs.When I fill the data into the TWs, I surround it with:
ui->itemList->clear(); ui->itemList->clearSelection(); ui->itemList->blockSignals(true); // Fill data ui->itemList->blockSignals(false);(
itemList= one of my TWs)While
clearSelectiondoes clear the selection for the code (ui->itemTree->selectedItems().isEmpty()) it does not do that visually
The item looks selected, but isn't according to the code
bool noItemSel = ui->itemList->selectedItems().isEmpty();This variable gets used to enable/disable certain buttons which should only work if an item is selected:
ui->pbItemAdd->setDisabled(noTreeRights); ui->pbItemEdit->setDisabled(noItemSel || noTreeRights); ui->pbItemDelete->setDisabled(noItemSel || noTreeRights);these buttons, shown here

get disabled,noTreeRightsis false, as seen by the add button, so that one doesnt cause the issue of disabling. Theres a lot of other loading done when someone selects an Item, so I'd rather keep the tree widget unselected than selecting the first itemSo how do I remove that visual selection?
@GKLC
Thanks. Am I overlooking something? I don’t find a question in your post! -
@GKLC
Thanks. Am I overlooking something? I don’t find a question in your post!@Axel-Spoerl I added the obvious question now.
-
@Axel-Spoerl I added the obvious question now.
@GKLC that item is probably the "currentItem"
and since your selection mode is notNoSelectionthe item is also "selected" aka highlighted -
@GKLC that item is probably the "currentItem"
and since your selection mode is notNoSelectionthe item is also "selected" aka highlighted -
@GKLC
While you wait for an answer, what about a bit more detective work?- Move the
clearSelection()to after theblockSignals()and data filling? - Can you temporarily comment out the
blockSignals(), does that change behaviour? - Try selecting a different item in code. (Note: I think that requires changing the selection, not the currrent item.) Does the blue item move?
- Is there any chance the first item in the list gets auto-/re-selected when first shown, independent of your code setting (I don't know)?
- Try moving any selecting or deselecting code into a slot executed on a
QTimer::singleShot()with a delay of 0 or small to execute after your filling code. Does that work?
- Move the
-
@GKLC
While you wait for an answer, what about a bit more detective work?- Move the
clearSelection()to after theblockSignals()and data filling? - Can you temporarily comment out the
blockSignals(), does that change behaviour? - Try selecting a different item in code. (Note: I think that requires changing the selection, not the currrent item.) Does the blue item move?
- Is there any chance the first item in the list gets auto-/re-selected when first shown, independent of your code setting (I don't know)?
- Try moving any selecting or deselecting code into a slot executed on a
QTimer::singleShot()with a delay of 0 or small to execute after your filling code. Does that work?
- Move the
-
@GKLC
While you wait for an answer, what about a bit more detective work?- Move the
clearSelection()to after theblockSignals()and data filling? - Can you temporarily comment out the
blockSignals(), does that change behaviour? - Try selecting a different item in code. (Note: I think that requires changing the selection, not the currrent item.) Does the blue item move?
- Is there any chance the first item in the list gets auto-/re-selected when first shown, independent of your code setting (I don't know)?
- Try moving any selecting or deselecting code into a slot executed on a
QTimer::singleShot()with a delay of 0 or small to execute after your filling code. Does that work?
First of all, as @J-Hilk pointed out, disable selections by
myTreeview->setSelectionMode(QAbstractItemView::NoSelection);Nothing can be selected from now on.
What may look a bit like a selection is thecurrentItem.
That should not be highlighted, unless the tree widget has focus.
It is usually rendered with a visible frame, indicating that the frame can be moved with the up/down keys and the item can be expanded/collapsed with the left/right keys. - Move the
-
First of all, as @J-Hilk pointed out, disable selections by
myTreeview->setSelectionMode(QAbstractItemView::NoSelection);Nothing can be selected from now on.
What may look a bit like a selection is thecurrentItem.
That should not be highlighted, unless the tree widget has focus.
It is usually rendered with a visible frame, indicating that the frame can be moved with the up/down keys and the item can be expanded/collapsed with the left/right keys.@Axel-Spoerl I've tried setting the current item to a nullptr, which didn't help
I do need to be able to set a selection (
SingleSelectionspecifically), at the start of the program however, I don't want anything selected, as it makes it confusing for users.Its also definitely a selection if the current item only has that frame, that is, if the selected item is the highlighted blue item shown in the picture
-
@Axel-Spoerl I've tried setting the current item to a nullptr, which didn't help
I do need to be able to set a selection (
SingleSelectionspecifically), at the start of the program however, I don't want anything selected, as it makes it confusing for users.Its also definitely a selection if the current item only has that frame, that is, if the selected item is the highlighted blue item shown in the picture
@GKLC
By now I suggest you produce a 20-odd line standalone minimal example. Reduce to absolute least which produces behaviour. May not apply to you, but 50% of the time we ask for this the OP discovers the issue. Try for yourself just starting anew from aQTreeWidget.