When the spacebar is pressed, the keyPressEvent
of QComboBox is called, and if it is not editable it automatically shows the popup (the same happens with F4 and Alt+↓). If another key is pressed and that key is not used for "keyboard navigation" such as the arrow keys, the keyboard search (not "autocompletion", which is a completely different thing) is activated.
Keyboard search uses the keyboardSearch()
function of the popup, so the solution is to implement a similar method.
This can be achieved with a subclass:
class MyCombo(QtWidgets.QComboBox):
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Space:
# get the QModelIndex of the underlying model
currentModelIndex = self.model().index(
self.currentIndex(), self.modelColumn(), self.rootModelIndex())
# get the popup view, which uses the same model
view = self.view()
# set the model index on the view
view.setCurrentIndex(currentModelIndex)
# call the keyboard search with the space character
view.keyboardSearch(' ')
else:
# use the default implementation if any other key is pressed
super().keyPressEvent(event)
Since you are probably using an UI generated by designer, the easiest solution would be to install an event filter on the combo. The implementation is almost the same.
class MyWindow(QtWidgets.QMainWindow):
def __init__(self):
# ...
self.ui.comboBox_ui_3.installEventFilter(self)
def eventFilter(self, source, event):
if source == self.ui.comboBox_ui_3 and event.type() == event.KeyPress:
if event.key() == QtCore.Qt.Key_Space:
currentModelIndex = source.model().index(
source.currentIndex(), source.modelColumn(), source.rootModelIndex())
view = source.view()
view.setCurrentIndex(currentModelIndex)
view.keyboardSearch(' ')
return True
return super().eventFilter(source, event)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…