Search code examples
pythonqtdesigner

qt QLineEdit from designer not responding to textChanged


I created a GUI with qt designer (PySide6) with a QLineEdit object 'lineEdit_r0' that for debugging purposes should just print something. For some reason, the textChanged signal is emitted only when the frame is initialized, but not when it is changed. I also have a working code example, but I cannot see the difference, besides that the example creates the window manually and not with the designer.

my code:

class FrmMain(QMainWindow, Ui_Hauptfenster):

    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.show()
        self.menu_Trap_Information.triggered.connect(self.open_trap_information)

    def open_trap_information(self):
        frm_trap_information.show()


class FrmTrapInformation(QMainWindow, Ui_Trap_Information):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.lineEdit_r0.textChanged[str].connect(self.update_trap_information_electrodes())

    def update_trap_information_electrodes(self):
        print("test")

app = QApplication(sys.argv)
frm_main = FrmMain()
frm_trap_information = FrmTrapInformation()
sys.exit(app.exec())

working example:

class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        hbox = QVBoxLayout(self)

        self.lbl = QLabel(self)
        qle = QLineEdit(self)

        qle.textChanged[str].connect(self.onChanged)

        hbox.addWidget(self.lbl)
        hbox.addSpacing(20)
        hbox.addWidget(qle)

        self.resize(250, 200)
        self.setWindowTitle('QLineEdit')
        self.show()

    def onChanged(self, text):
        print("something changed!")
        self.lbl.setText(text)
        self.lbl.adjustSize()


app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec())

Solution

  • The main mistake was that the method that is passed to the connect must not contain parentheses. It has to be:

    self.lineEdit_r0.textChanged[str].connect(self.update_trap_information_electrodes)
    

    With the lambda function and passing additional arguments it can be e.g.:

    self.lineEdit_r0.textChanged[str].connect(lambda text: self.update_trap_information_electrodes(text, "r0"))
    

    In this case text would be the value that is entered in the QLineedit and "r0" the additional argument.