以下代码在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)
答案 0 :(得分:1)
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)
编辑:根据以下评论改编的解决方案。
答案 1 :(得分:0)
这在PyQt5中变得更加简单:
self.dataChanged.emit(index, index, [QtCore.Qt.EditRole])