编程语言
首页 > 编程语言> > python – 在Pyqt4中不触发的textChanged事件

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

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

解决方法:

替换以下行:

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
来源: https://codeday.me/bug/20190725/1532571.html