3

I'm trying to understand why QLineEdit "editingFinished" signals are generated when other widgets are selected. In the example below the "on_lineedit" method is called when the combo box is selected. Why?

ui example

import sys
from PyQt5 import QtWidgets

class MyApp(QtWidgets.QDialog):
    def __init__(self, *args):
        super().__init__(*args)
        # create combobox:
        combobox = QtWidgets.QComboBox(self)
        combobox.addItems(['Item 1', 'Item 2'])
        # create line edit
        lineedit = QtWidgets.QLineEdit(self)
        lineedit.editingFinished.connect(self.on_lineedit)
        # layout:
        vbox = QtWidgets.QVBoxLayout()
        vbox.addWidget( combobox )
        vbox.addWidget( lineedit )
        self.setLayout(vbox)

    def on_lineedit(self):
        print('on_lineedit')

app    = QtWidgets.QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())

I know that this issue can be avoided by connecting the "textChanged" signal instead of the "editingFinished" signal like this:

lineedit.textChanged.connect(self.on_lineedit)

and I've seen similar issues raised elsewhere (links below) but I still don't understand why the "editingFinished" signal is generated when the combobox is selected.

Qt qspinbox editingFinished signal on value changed

Suppress QLineEdit editingFinished signal when certain button is clicked

aL_eX
  • 1,453
  • 2
  • 15
  • 30
ToddP
  • 652
  • 13
  • 18
  • I only use that behavior when the focus is placed in QLineEdit and then I press the QComboBox and the editingFinished broadcast is appropriate since the editing started when I put the focus in the QLineEdit and it ends when I change the focus to another widget. Could you describe how that behavior happens? your description is ambiguous. – eyllanesc Jan 29 '18 at 23:38
  • It works that way because someone filling out a form will tab to the next field once they've finished editing the current one (but they may not always press return/enter before doing so). The `textChanged` signal is completely different, since it is continually emitted *during* editing, but never *afterwards*. – ekhumoro Jan 30 '18 at 02:32

1 Answers1

4

From http://doc.qt.io/archives/qt-4.8/qlineedit.html#editingFinished

This signal is emitted when the Return or Enter key is pressed or the line edit loses focus.

The signal is emitted because it is designed to be. The other widget your click on is not really relevant here, what is relevant is that it the line edit loses focus and it is that which causes the signal to be emitted. Clicking on another widget is just one of many ways your line edit might lose focus.

three_pineapples
  • 11,579
  • 5
  • 38
  • 75
  • This is clear for the first click, but then why does the lineEdit retain focus after clicking on the other widget? – ToddP Jan 30 '18 at 05:39
  • @ToddP I think the default focus policy for combo boxes is `Qt.NoFocus`, so focus returns to the line edit. You may want to investigate a slightly more complex example where you have a combo box and two line edits to see which one emits the `editingFinished` signal at which time, and how focus returns depending on the focus policy (which you can change) of each widget. – three_pineapples Jan 30 '18 at 07:05