slalomchip
slalomchip

Reputation: 909

PyQt Dual Purpose ENTER Key Press

I’m using PyQt5 and Python 3.6. I want to use the ENTER (or RETURN) key for dual purposes.

If the user enters text into the combo box and then hits the ENTER key, then I want the text from the combo box to be appended to a list. In all other situations, I want the ENTER key to serve as a shortcut for a push button.

I can’t find the correct decision for how the handle when ENTER is pressed. Here is a code sample. I’m looking to a decision in the returnDecision(self) function (toward the bottom of the script).

import sys
from PyQt5.QtWidgets import QWidget, QPushButton, QApplication, QShortcut
from PyQt5.QtWidgets import QComboBox
from PyQt5.QtGui import QKeySequence   
from PyQt5.QtCore import Qt, QSize

class Example(QWidget):

    def __init__(self):
        super().__init__()        
        self.initUI()        

    def initUI(self):
        self.setGeometry(300, 300, 300, 200)        
        btn = QPushButton('Button', self)
        btn.move(100, 50)               
        btn.clicked.connect(self.btnPrint)
        self.comboBox = QComboBox(self)
        self.comboBox.setEditable(True)
        self.comboBox.move(100, 150)
        self.comboBox.setMinimumSize(QSize(150, 0))
        self.comboBox.setEditText("Initial Text")
        self.comboBox.editTextChanged.connect(self.cboxPrint)
        enter = QShortcut(QKeySequence(Qt.Key_Return), self)
        enter.activated.connect(self.returnDecision)
        self.textList = []
        self.show()

    def btnPrint(self):
        print("Button was pressed")

    def btnAction(self):
        print("RETURN pressed when NOT editing combo box")
        self.btnPrint()

    def cboxPrint(self):
        print(self.comboBox.currentText())

    def cboxAction(self):
        print("RETURN pressed when editing combo box")
        self.textList.append(self.comboBox.currentText())
        print(self.textList)        

    def returnDecision(self):
        if ENTER KEY WAS PRESSED WHILE EDITING COMBO BOX:
            self.cboxAction()
        else:
            self.btnAction()

if __name__ == '__main__':    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

Any suggestions?

Upvotes: 1

Views: 4750

Answers (1)

MalloyDelacroix
MalloyDelacroix

Reputation: 2293

One way to solve this is to use a custom subclass of the QComboBox and override the keyPressEvent method. Then also implement a keyPressEvent in your widget and handle each differently.

class CustomCombo(QtWidgets.QComboBox):

    enter_pressed = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        super().__init__(parent)

    def keyPressEvent(self, event):
        if event.key() == QtCore.Qt.Key_Return:
            self.enter_pressed.emit()
        else:
            QtWidgets.QComboBox.keyPressEvent(self, event)  
            # if the key is not return, handle normally


class Example(QWidget):

    def __init__(self):
        # Code here
        self.combo_box = CustomCombo(self)
        self.combo_box.enter_pressed.connect(self.cboxAction)

    def keyPressEvent(self, event):
        if event.key() == QtCore.Qt.Key_Return:
            self.btnAction()

Upvotes: 2

Related Questions