简体   繁体   中英

QcomboBox using “ENTER” event

在此处输入图片说明

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.

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_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM