How to emit dataChanged in PyQt5

The code below breaks into the line self.emit. It works great in PyQt4. How to fix this code so that 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)  

Edited later:

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)
+4
source share
1 answer

As you can read here , it QtCore.SIGNALwas canceled after PyQt4and therefore incompatible.

This page explains the new style cues and slots for PyQt5. Syntax:

PyQt5.QtCore.pyqtSignal(types[, name[, revision=0[, arguments=[]]]])

Your case can be transferred to:

from PyQt5 import pyqtsignal

data_changed = pyqtsignal(QModelindex,QModelIndex)

and emit your signal:

self.data_changed.emit(index, index)

Edit: The solution is adapted from the comments below.

+1
source

All Articles