下面的代码在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)
最佳答案
As you can read here,QtCore.SIGNAL
在PyQt4
之后停产,因此不兼容。
This page解释了PyQt5
的新型信号和插槽。语法为:
PyQt5.QtCore.pyqtSignal(types[, name[, revision=0[, arguments=[]]]])
您的案件可以翻译成:
from PyQt5 import pyqtsignal
data_changed = pyqtsignal(QModelindex,QModelIndex)
并发出您的信号:
self.data_changed.emit(index, index)
编辑:从下面的评论改编的解决方案。