Search code examples
pythonpython-2.7pyqtpyqt4qcombobox

QcomboBox using "ENTER" event


enter image description here

in this user selects item from DROP DOWN menu use "SEARCH" button to search. I want to add "ENTER" but as a shortcut for this event. Please refer image.it will be more clear.


Solution

  • A simple solution is to use a QShortcut as I show below:

    from PyQt4 import QtGui, QtCore
    
    class Widget(QtGui.QWidget):
        def __init__(self, parent=None):
            QtGui.QWidget.__init__(self, parent)
            lay = QtGui.QHBoxLayout(self)
            combo = QtGui.QComboBox()
            combo.addItems(["option1", "option2", "option3"])
            lay.addWidget(combo)
            lay.addWidget(QtGui.QPushButton("Press Me"))
    
            shortcut = QtGui.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Return), combo, activated=self.onActivated)
    
        def onActivated(self):
            print("enter pressed")
    
    if __name__ == "__main__":
        import sys
        app = QtGui.QApplication(sys.argv)
        w = Widget()
        w.show()
        sys.exit(app.exec_())