4

How come the textChanged event is not happening whenever I input some data in the QLineEdit?

from PyQt4.Qt import Qt, QObject,QLineEdit
from PyQt4.QtCore import pyqtSlot,SIGNAL,SLOT
from PyQt4 import QtGui, QtCore

import sys


class DirLineEdit(QLineEdit, QtCore.QObject):
"""docstring for DirLineEdit"""

@pyqtSlot(QtCore.QString)
def textChanged(self, string):
        QtGui.QMessageBox.information(self,"Hello!","Current String is:\n"+string)  

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

    self.connect(self,SIGNAL("textChanged(QString&)"),
                self,SLOT("textChanged(QString *)"))


app = QtGui.QApplication(sys.argv)
smObj = DirLineEdit()

smObj.show()

app.exec_()

everything seems to be correct to me, what am I missing ?

Ciasto piekarz
  • 7,853
  • 18
  • 101
  • 197

1 Answers1

8

Replace following line:

self.connect(self,SIGNAL("textChanged(QString&)"),
            self,SLOT("textChanged(QString *)"))

with:

self.connect(self,SIGNAL("textChanged(QString)"),
            self,SLOT("textChanged(QString)"))

Or you can use self.textChanged.connect (handler should be renamed because the name conflicts):

class DirLineEdit(QLineEdit, QtCore.QObject):

    def on_text_changed(self, string):
            QtGui.QMessageBox.information(self,"Hello!","Current String is:\n"+string)  

    def __init__(self):
        super(DirLineEdit, self).__init__()
        self.textChanged.connect(self.on_text_changed)
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • 3
    Oouch I already got it working like this `self.textChanged.connect(self.on_text_changed)` – Ciasto piekarz Nov 03 '13 at 08:50
  • @san Your comment should be the accepted answer! Also +1 for: "Or you can use ``self.textChanged.connect``" although it is not as complete as the solution in the comment. – Nras Oct 21 '14 at 08:05
  • `self.lineEdit.connect(self.lineEdit,QtCore.SIGNAL("menu()"), self.lineEdit,QtCore.SLOT("menu()"))` raises `Object::connect: No such slot QLineEdit::menu()` error – Hilal Oct 13 '16 at 10:03