The code below breaks on self.emit line. It works fine in PyQt4. How to fix this code so it works in 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)
A working solution:
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
was discontinued after PyQt4
and is therefore not compatible.
This page explains the new-style signals and slots for PyQt5
. The syntax is:
PyQt5.QtCore.pyqtSignal(types[, name[, revision=0[, arguments=[]]]])
Your case could be translated to:
from PyQt5 import pyqtsignal
data_changed = pyqtsignal(QModelindex,QModelIndex)
and to emit your signal:
self.data_changed.emit(index, index)
Edit: Solution adapted from comments below.
这在 PyQt5 中变得更加简单:
self.dataChanged.emit(index, index, [QtCore.Qt.EditRole])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.