smg
smg

Reputation: 37

Moving focus to next QLineEdit using ENTER

I wrote an app to calculate amount of money by enter amount of coins and bank notes to calculate total value. I can jump to next text field using 'tab' key but I would rather to use 'enter' key. How can I set 'enter' instead of default 'tab' key for moving to next QLineEdit field ?

ScreenShot

Upvotes: 1

Views: 1734

Answers (1)

eyllanesc
eyllanesc

Reputation: 243945

You have to override the event() method so that when it detects the KeyPress event and the keys are Enter then call focusNextPrevChild():

import sys

from PyQt5 import QtCore, QtWidgets


class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        lay = QtWidgets.QVBoxLayout(self)

        for i in range(10):
            le = QtWidgets.QLineEdit()
            lay.addWidget(le)

    def event(self, event):
        if event.type() == QtCore.QEvent.KeyPress:
            if event.key() in (QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter):
                self.focusNextPrevChild(True)
        return super().event(event)


app = QtWidgets.QApplication(sys.argv)

w = Widget()
w.show()

sys.exit(app.exec_())

Upvotes: 2

Related Questions