下面的代码在self.emit这一行出错。在PyQt4中运行良好。如何修改代码使其在PyQt5中正常工作?
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QObject, pyqtSignal
class ItemDelegate(QtWidgets.QItemDelegate):
def __init__(self, parent):
QtWidgets.QItemDelegate.__init__(self, parent)
def createEditor(self, parent, option, index):
return QtWidgets.QLineEdit()
@QtCore.pyqtSlot()
def setModelData(self, editor, model, index):
self.emit(QtCore.SIGNAL("dataChanged(QModelIndex,QModelIndex)"), index, index)
稍后编辑:
一个可行的解决方案:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QObject, pyqtSignal
class Communicate(QObject):
data_changed = pyqtSignal(QtCore.QModelIndex, QtCore.QModelIndex)
class ItemDelegate(QtWidgets.QItemDelegate):
def __init__(self, parent):
QtWidgets.QItemDelegate.__init__(self, parent)
self.c = Communicate()
@QtCore.pyqtSlot()
def setModelData(self, editor, model, index):
self.c.data_changed.emit(index, index)
pyqtSlot
装饰器也应该给出类型参数以反映setModelData
的参数。旧版本的 PyQt5 将会建立连接,但 PyQt 5.6 现在强制执行此规定。 - user3419537