Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Return a function when arrow up key pressed?!



  • I have this line code for when the enter is pressed on the line edit. But, how to convert this code to call the function if the arrow up key is pressed on the line edit?

    self.lineEdit.returnPressed.connect(self.call_sc)
    
    def call_sc(self):
        print('working')
    

    Representation

    self.lineEdit.Key_upPressed.connect(self.call_sc)
    
    def call_sc(self):
        print('working')
    


  • @Black-Cat There is no default signal that is emitted when the up key is pressed, so you will have to create it and for this an option is to override the keyPressEvent method:

    import sys
    
    from PySide2 import QtCore, QtWidgets
    
    
    class LineEdit(QtWidgets.QLineEdit):
        upPressed = QtCore.Signal()
    
        def keyPressEvent(self, event):
            super().keyPressEvent(event)
            if event.key() == QtCore.Qt.Key_Up:
                self.upPressed.emit()
    
    
    class Widget(QtWidgets.QWidget):
        def __init__(self, parent=None):
            super().__init__(parent)
    
            lineedit = LineEdit()
    
            lay = QtWidgets.QVBoxLayout(self)
            lay.addWidget(lineedit)
            lay.addStretch()
    
            lineedit.upPressed.connect(self.call_sc)
    
        def call_sc(self):
            print('working')
    
    
    if __name__ == "__main__":
        app = QtWidgets.QApplication(sys.argv)
        w = Widget()
        w.show()
        sys.exit(app.exec_())
    

    Another option is to implement it through an event filter:

    import sys
    
    from PySide2 import QtCore, QtWidgets
    
    
    class Widget(QtWidgets.QWidget):
        def __init__(self, parent=None):
            super().__init__(parent)
    
            self.lineedit = QtWidgets.QLineEdit()
    
            lay = QtWidgets.QVBoxLayout(self)
            lay.addWidget(self.lineedit)
            lay.addStretch()
    
            self.lineedit.installEventFilter(self)
    
        def eventFilter(self, obj, event):
            if obj is self.lineedit and event.type() == QtCore.QEvent.KeyPress:
                if event.key() == QtCore.Qt.Key_Up:
                    self.call_sc()
            return super().eventFilter(obj, event)
    
        def call_sc(self):
            print("working")
    
    
    if __name__ == "__main__":
        app = QtWidgets.QApplication(sys.argv)
        w = Widget()
        w.show()
        sys.exit(app.exec_())
    


  • @eyllanesc Thank you man



  • @eyllanesc
    Basicly I need to implemente this key press reaction in a script that I will call by another script, so, it don't have the "if name == "main":"

    I tried some changes but it return the same erro

            self.lineEdit.installEventFilter(self)
    
        def eventFilter(self, obj, event):
            if obj is self.lineEdit and event.type() == QtCore.QEvent.KeyPress:
                if event.key() == QtCore.Qt.Key_Up:
                    self.call_sc()
            return super().eventFilter(obj, event)
    

    00f4892b-ad5c-4241-afb5-d5ced8f21837-image.png

    What is supposed to input in this arguments on line 1053?

    self.lineEdit.installEventFilter(self)
    


  • @Black-Cat With the little code it is impossible to help you so if you want more help then you must provide a minimal and reproducible example


Log in to reply