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. TreeView “cursor” without focus?
QtWS25 Last Chance

TreeView “cursor” without focus?

Scheduled Pinned Locked Moved Solved General and Desktop
currentindexfocuscursorqabstractitemvitreeview
10 Posts 3 Posters 2.9k Views
  • 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.
  • - Offline
    - Offline
    -DLF-
    wrote on 17 Jan 2019, 19:11 last edited by
    #1

    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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 17 Jan 2019, 20:16 last edited by SGaist
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • - Offline
        - Offline
        -DLF-
        wrote on 17 Jan 2019, 20:50 last edited by
        #3

        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.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 17 Jan 2019, 20:58 last edited by
          #4

          What do you mean by cursor ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • - Offline
            - Offline
            -DLF-
            wrote on 17 Jan 2019, 21:47 last edited by
            #5

            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.

            1 Reply Last reply
            0
            • - Offline
              - Offline
              -DLF-
              wrote on 17 Jan 2019, 22:07 last edited by
              #6

              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…

              M 1 Reply Last reply 17 Jan 2019, 23:04
              0
              • - -DLF-
                17 Jan 2019, 22:07

                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…

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 17 Jan 2019, 23:04 last edited by
                #7

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

                1 Reply Last reply
                2
                • - Offline
                  - Offline
                  -DLF-
                  wrote on 18 Jan 2019, 17:53 last edited by -DLF-
                  #8

                  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…

                  M 1 Reply Last reply 18 Jan 2019, 18:51
                  0
                  • - -DLF-
                    18 Jan 2019, 17:53

                    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…

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 18 Jan 2019, 18:51 last edited by mrjj
                    #9

                    @-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

                    1 Reply Last reply
                    2
                    • - Offline
                      - Offline
                      -DLF-
                      wrote on 19 Jan 2019, 21:27 last edited by
                      #10

                      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!
                      Dan

                      1 Reply Last reply
                      1

                      1/10

                      17 Jan 2019, 19:11

                      • Login

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