How to capture the Key_tab event

后端 未结 1 1875
感动是毒
感动是毒 2021-01-23 05:53

i am trying to capture the key_tab event, but no luck. i realized that it only works if there are no other widgets so the cursor has no where to go only then can i get the event

相关标签:
1条回答
  • 2021-01-23 06:40

    Apparently the Key_Tab press event is never passed to any handler but to the setFocus() so in order to intercept the Key_Tab event, we need to implement the event() method itself. so here is the new code:

    class MyCombo(QComboBox):
    
        def __init__(self, parent=None):
            super(MyCombo, self).__init__(parent)
            self.setEditable(True)
    
        def keyPressEvent(self, event):
            if event.key() == Qt.Key_Return:
                print "return pressed"
            else:
                QComboBox.keyPressEvent(self, event)
    
        def event(self, event):
            if event.type() == QEvent.KeyPress and event.key() == Qt.Key_Tab:
                print "tab pressed"
                return False
            return QWidget.event(self, event)
    
    class Form_1(QDialog):
    
        def __init__(self, parent=None):
            super(Form_1, self).__init__(parent)
            self.combo = MyCombo()
            self.line = QLineEdit()
            layout = QVBoxLayout()
            layout.addWidget(self.combo)
            layout.addWidget(self.line)
            self.setLayout(layout)
    
    0 讨论(0)
提交回复
热议问题