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. How to change QCombobox popup highlight position when press a key

How to change QCombobox popup highlight position when press a key

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 716 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.
  • D Offline
    D Offline
    ducdx
    wrote on last edited by
    #1

    Hi all,
    I have an issue with QCombobox that need help from you. I make a desktop application that includes some QCombobox in it. I've done all of the work except for a problem with a keyboard event. Detail as below:
    1 - Press Enter key -> show the list item assigned to the Combobox
    2 - Press the U/D key -> change selection in the list item
    3 - Press Enter key again -> set highlighted item to the currently selected item in the Combobox
    I've done (1). With (2), the default Up/Down Key can perform this, but now I need to custom with another key instead of the default key. Unfortunately, I don't know the event or function that can change the selection position in the popup view like when the Up/Down key was pressed. Could you help me? Thank you

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You can use an event filter that you install on the QCombBox's view. Or create your own QListView subclass that you modify to handle the keyboard the way you want.

      Out of curiosity, why do you need to use a different key ?

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

      D 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        You can use an event filter that you install on the QCombBox's view. Or create your own QListView subclass that you modify to handle the keyboard the way you want.

        Out of curiosity, why do you need to use a different key ?

        D Offline
        D Offline
        ducdx
        wrote on last edited by
        #3

        @SGaist Thank you for your answer very much

        Out of curiosity, why do you need to use a different key?

        Related to your question, here is my answer:

        • The first, I try to use the default key (left/right/down/up) to navigate and control the Combobox, but I faced a problem as below:
          • When the Up/Down arrow key was pressed -> switch focus to the next Widget on the screen -> I can do it, but the problem was that the current index of the Combobox was also changed to the up/down item corresponding to the key. I have tried many ways but I can't prevent the Combobox change the current index
          • After that, I try to use another key to navigate -> it's ok to switch focus to another Widget, but the problem was that I can't change the highlighted item after the popup was shown as the topic
            Could you please give me a bit of advice on that should I continue using the default key or another key? If it's the first one, how can I prevent the Combobox change its current index when the up/down key was pressed? Thank you very much.
        1 Reply Last reply
        0
        • D Offline
          D Offline
          ducdx
          wrote on last edited by
          #4

          Thanks all, I've resolved the problem
          Here is my solution:

          • subclass of QCombobox, and setFocusPolicy to TabFocus
          • Reimplement keyPressEvent and ignore the event
          • Reimplement keyReleaseEvent and accept the event that I want to handle
              self.setFocusPolicy(QtCore.Qt.FocusPolicy.TabFocus)
              def keyPressEvent(self, event: QtGui.QKeyEvent) -> None:
                  event.ignore()
                  return
          
              def keyReleaseEvent(self, event: QtGui.QKeyEvent) -> None:
                  if event.key() == QtCore.Qt.Key_Return:
                      self.showPopup()
                      return
                  else:
                      event.ignore()
                  super(DLComboBox, self).keyReleaseEvent(event)
          
          1 Reply Last reply
          1

          • Login

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