【问题标题】:PyQt Dual Purpose ENTER Key PressPyQt 两用 ENTER 键按下
【发布时间】:2018-08-05 07:52:59
【问题描述】:

我正在使用 PyQt5 和 Python 3.6。我想将ENTER(或RETURN)键用于双重目的。

如果用户在组合框中输入文本,然后点击ENTER 键,那么我希望将组合框中的文本附加到列表中。在所有其他情况下,我希望 ENTER 键用作按钮的快捷方式。

当ENTER 被按下时,我找不到正确的决定。这是一个代码示例。我正在寻找returnDecision(self) 函数中的决定(朝向脚本的底部)。

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

有什么建议吗?

【问题讨论】:

    标签: python pyqt pyqt5 enter qcombobox


    【解决方案1】:

    解决此问题的一种方法是使用 QComboBox 的自定义子类并覆盖 keyPressEvent 方法。然后还在您的小部件中实现一个 keyPressEvent 并以不同方式处理。

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

    【讨论】:

    • 我喜欢你关于自定义子类的想法,但我在实现它时遇到了困难。我在“示例”中定义了comboBox 并尝试了但似乎无法将信号传递给CustomCombo 类。我尝试了self.comboBox.editTextChanged.connect(self.combo_box(Qt.SIGNAL("pressed()"))),但似乎从 PyQt5 开始不允许动态分配的信号。我可以将按键信号连接到ComboBox 中的另一种方法,但是当按下 RETURN 键时,Qt.Key_Return 总是在Example 中截获self.keyPressEvent()(而不是CustomCombo)。有什么建议吗?
    • 我不明白。您想将信号从您的 Example 类传递给 CustomCombo 类吗? self.cboxAction 和 self.btnAction 不是在 Example 类中定义的吗?如果是这样,您将需要从 CustomCombo 类到 Example 类的信号,这就是我的回答。
    • 好的。你说的对。仍然在继承、超类、子类等方面磕磕绊绊。但我认为我拥有它(现在)。还有一个问题 - CustomCombo(QComboBox) 小部件没有显示在带有self.show() 的主窗口中 - 只有按钮显示。如果我添加self.combo_box.show(),小部件将显示在不同的窗口中,但否则会正常工作。我完全按照你写的方式实现了CustomCombo(QComboBox)。我在示例的initUI(self) 中有self.combo_box = CustomCombo(),然后是它的初始化(大小、几何形状等)。如何使 CustomCombo 小部件显示?
    • 抱歉,我的测试初始化​​设置与您的不同。在您的情况下,您需要将 CustomCombo 类设置为采用父参数并将其提供给 QComboBox 超级调用。然后在定义 self.comboBox 的 Example 类中为其提供一个 self 实例。 self.comboBox = CustomCombo(self)。我已经编辑了我的答案以反映这些变化。
    • 行得通!我觉得它必须与继承有关。
    猜你喜欢
    • 2019-02-22
    • 1970-01-01
    • 2014-04-04
    • 2018-02-13
    • 2014-09-20
    • 2014-12-18
    • 1970-01-01
    • 2011-08-03
    • 2015-09-25
    相关资源
    最近更新 更多