Problems with Open-Source Downloads read and

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.


  • Lifetime Qt Champion

    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.

  • Hi @SGaist, thanks,
    I thought it would be easier somehow, but I will check for your idea.

    I'm on Linux. The selected items stay visible, just the “current-cursor” does not.

  • Lifetime Qt Champion

    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:

       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…

  • Lifetime Qt Champion

    The default selection is one cell, so i wonder if you set
    treeview->setSelectionBehavior (QAbstractItemView::SelectRows)
    its more what you like ?

  • 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:
    alt text

    I guess I can solve my problem by rendering the item with the current index with a different background (QAbstractItemModel::data with the Qt.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…

  • Lifetime Qt Champion

    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.

  • 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 to Qt.NoFocus. I draw the “cursor” then myself by giving a different backlog in QAbstractItemModel::data. That makes anyway sense since the cursor position is part of my domain model.

    Thanks for help and inspiration!