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