textChanged事件在Pyqt4中没有触发

时间:2013-11-03 07:47:56

标签: python pyqt4 textchanged

为什么每当我在QLineEdit中输入一些数据时都不会发生textChanged事件?

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_()

对我来说,一切似乎都是对的,我错过了什么?

1 个答案:

答案 0 :(得分:8)

替换以下行:

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

使用:

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

或者您可以使用self.textChanged.connect(由于名称冲突,应重命名处理程序):

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)