Diogo Magalhães
Diogo Magalhães

Reputation: 461

Pyqt: How to get a signal that QLineEdit is on focus?

This specific window has 2 QLineEdits: one called "User" and the other called "Password". Below them there is a keyboard created with many QPushButtons. Now, I have to tell the method connected to those buttons to print the respective letter or number in the selected QLineEdit and I'm having a hard time getting the signal for which QLineEdit is selected at the moment.

How do I do I get that signal? The application starts with "User" focused: self.User.setFocus()

Thank you.

Upvotes: 3

Views: 3967

Answers (1)

Brendan Abel
Brendan Abel

Reputation: 37569

By default, the buttons will gain focus when you click them. You can change this by setting their focus policy to NoFocus. This way, your lineedits won't lose focus when users click the buttons.

 button.setFocusPolicy(QtCore.Qt.NoFocus)

As long as the lineedit had focus when the button was clicked, you can get the focused widget in your button handler like this:

lineedit = QtGui.QApplication.focusWidget()

You could check to make sure the focused widget is one of the two lineedit widgets and then update their text accordingly.

Upvotes: 4

Related Questions