Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to assign a hotkey to a QCombobox?



  • Hello I have a Qcombobox. When I open its drop-down menu, is there a way to assign a hotkey to each menus?


  • Lifetime Qt Champion

    Hi,

    What behaviour do you have in mind ?



  • @SGaist said in How to assign a hotkey to a QCombobox?:

    Hi,

    What behaviour do you have in mind ?

    For example, when I open this drop-down box, I can press 'A' to select "Activated (A)".
    0_1535101740130_1.png



  • I think just calling the QComboBox::setCurrentIndex(int index) method should do the trick.

    If you just call it as a response to the key pressed, you should get the behavior you want.
    Now there is the question: who detects the keypress ? Should those hotkeys always be active, no matter which widget has focus ? If that's what you want, you can set an eventfilter on the whole application. Be carefull, though. As your application becomes bigger, global hotkeys probably quickly stop being a good idea.

    When I open this drop-down box

    If that's the only time the hotkey is active, then you can set the filter on the QComboBox alone, or (cleaner) inherit from QComboBox to overwrite QWidget::keyPressEvent.


Log in to reply