每当我在 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_()

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

最佳答案

替换以下行:

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)

关于python - Pyqt4 中未触发 textChanged 事件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19750890/

10-14 18:31