key.pressEvent (), does not execute the indicated task

2

I would like to know how I can execute a function by pressing the enter key, in a specific widget.

For this case, I am trying to execute a function at the moment of pressing enter in the qlineedit. but it does not answer this is the image of the interface

this is the code:

from PyQt5.QtWidgets import QMainWindow,QApplication, QAction
from PyQt5 import uic
from PyQt5.QtGui import *
from PyQt5.QtCore import QEvent


class Primera(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        uic.loadUi("Inicio.ui",self)

    def keyPressEvent(self,obj,event):
        if obj is self.l_codigo:
            if event.key() == QtCore.Qt.Key_Return:
                self.setFocus(self.edit)
            event.accept()


app = QApplication([])
p = Primera()
p.show()
app.exec_()
    
asked by Revsky01 27.07.2018 в 05:59
source

1 answer

2

QLineEdit has the signal returnPressed that is issued when you press enter.

from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5 import uic


class Primera(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        uic.loadUi("Inicio.ui",self)
        self.l_codigo.returnPressed.connect(self.edit.setFocus)


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    p = Primera()
    p.show()
    sys.exit(app.exec_())

On the other hand when using keyPressEvent you are overwriting the event of Primera , that has nothing to do with the QLineEdit, if you want to use eventFilter (only use this method if the appropriate signal does not exist) you should do it in the following way :

from PyQt5.QtCore import QEvent, Qt
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5 import uic


class Primera(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        uic.loadUi("Inicio.ui",self)
        self.l_codigo.installEventFilter(self)

    def eventFilter(self,obj,event):
        if obj is self.l_codigo and event.type() == QEvent.KeyPress:
            if event.key() == Qt.Key_Return:
                self.edit.setFocus()
        return QMainWindow.eventFilter(self,obj,event)

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    p = Primera()
    p.show()
    sys.exit(app.exec_())

As you realize, the second method is less elegant, more extensive.

If we overwrite keyPressEvent () correctly, it should be as follows:

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5 import uic


class Primera(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        uic.loadUi("Inicio.ui",self)

    def keyPressEvent(self, event):
        if event.key() == Qt.Key_Return:
            print("event")
        return QMainWindow.keyPressEvent(self, event)

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    p = Primera()
    p.show()
    sys.exit(app.exec_())

But the problem is that it will be launched for all the widgets that are children of Principal

    
answered by 27.07.2018 / 06:13
source