TreeView “cursor” without focus?
-
Hi there! My first post in this forum is maybe a simple question:
QAbstractItemView’s show a “cursor” for the “current” entry or index. This cursor is only shown when the Widget has the focus.
Is it possible to make the widget show the cursor at any time, even when the widget doesn’t have the focus?
Background: My UI has additional options for the user to navigate through a tree view. As an easy example, think on a pair of up/down buttons to move the cursor. That implies that the tree does not always have the focus when the user navigates through and looks at the tree. But that of course doesn’t make sense if the user cannot see which tree entry is the current one.
Thanks!
Dan -
Hi and welcome to devnet,
IIRC, you can use a custom QStyledItemDelegate and re-implement the paint event to force using the main colour even when unfocused.
By the way, what OS are you working ? Usually when a QTreeView is not in focus, the select items are still visible with a lighter colour.
-
What do you mean by cursor ?
-
Sorry, I was not clear enough.
Well, as I understood (doing Qt since a week), each QAbstractItemView has a “current index” (QItemSelectionModel::currentIndex()
). The item which corresponds to this current index is highlighted by some kind of cursor which can be moved via the keyboard by the user.Since Qt directly selects an item when moving this cursor with the keyboard, it often cannot be seen (unless the widget has
QAbstractItemView::NoSelection
or the user deselects a clicked row with the Ctrl key).I'm now fighting with styling Qt to get this “cursor” always visible and to make the “currentIndex” (which determines the position of this cursor) and the selection completely controlled by the domain model of my application. (Yeah, what item’s are selected and to what item the “keyboard cursor” points to is really part of my domain logic.) So, the user interactions with my TreeView are not limited to the default interactions as they are predetermined by Qt. This is why everything has to work without the tree having the focus.
-
Oh, just to add:
QTreeView::item:focus { selection-background-color: yellow; }
makes the item with the current index (the “cursor”, as I called it) quite visible. Once the tree view looses the focus, this yellow color will disappear…
I'm also struggling with having this focus/cursor highlighting the complete row of my multi-column tree view and not only one cell. But maybe I could live without that…
-
Hi @mrjj ,
this seems to be a confusing thing. I do not talk about the selection but the “current index cursor” or focus, as it is called in QSS.See the cursor in cell (0,0) and a selection marker in the last row:
I guess I can solve my problem by rendering the item with the current index with a different background (
QAbstractItemModel::data
with theQt.BackgroundRole
). I got rid of the original “current index cursor” but setting the tree’s focus policy to Qt.NoFocus. That way, I will get full control of the representation, I hope… -
@-DLF-
hi
Indeed it is.
But it was the focus rect, its clear now.Focus Rect is not same as currently selected but indicates which would be selected if a trigger action is performed.
Anyway, yes setting NoFocus will then not show it but also disable navigation with keyboard.
But if thats acceptable, i guess its fine.Else its a delegate.
Its not that complicated.
https://www.qtcentre.org/threads/31694-Get-rid-of-the-stupid-frustrating-focus-rectangle-in-QTreeWidget -
Hi,
It seems I can go with the solution I described. I don't need the tree view to be focused, so I just removed the “focus rect” (if that is the name Qt people know ;)) by setting the focus policy of the tree toQt.NoFocus
. I draw the “cursor” then myself by giving a different backlog inQAbstractItemModel::data
. That makes anyway sense since the cursor position is part of my domain model.Thanks for help and inspiration!
Dan