4

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

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

4

1 回答 1

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)
于 2013-11-03T08:08:10.703 回答