Return a function when arrow up key pressed?!
-
wrote on 3 Jan 2021, 00:37 last edited by Black Cat 1 Mar 2021, 00:39
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')
-
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')
wrote on 3 Jan 2021, 01:26 last edited by eyllanesc 1 Mar 2021, 01:29@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_())
-
@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_())
wrote on 3 Jan 2021, 02:20 last edited by@eyllanesc Thank you man
-
@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_())
wrote on 3 Jan 2021, 15:14 last edited by@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)
What is supposed to input in this arguments on line 1053?
self.lineEdit.installEventFilter(self)
-
@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)
What is supposed to input in this arguments on line 1053?
self.lineEdit.installEventFilter(self)
wrote on 3 Jan 2021, 15:19 last edited by@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
1/5