Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQt5 connection doesn't work: item cannot be converted to PyQt5.QtCore.QObject in this context

I am trying to connect a signal from a created object and am getting an error. Here is a simplified version of my code:

class OverviewWindow(QMainWindow):
    def __init__(self, projectClusters, users, contributorDict, userLastRevPerProj):
        QMainWindow.__init__(self)
        # Code....

    def createUserNodes(self):
        userNodes = {}
        nodeSpread = 50
        yPos = -400
        nodeSpan = nodeSpread + 100
        width = (len(self.usersFilt) - 1) * nodeSpan
        xPos = 0 - (width / 2)

        for user in self.usersFilt:
            newItem = NodeItem(xPos, yPos, self.nodeDiameter, user, True)
            newItem.nodeDoubleClicked.connect(self.dc)
            userNodes[user] = newItem
            self.graphicsScene.addItem(newItem)
            xPos += nodeSpan

        return userNodes

    @pyqtSlot(str)
    def dc(self, text):
        print(text)


class NodeItem(QGraphicsItem):
    nodeDoubleClicked = pyqtSignal(str)

    def __init__(self, xPos, yPos, diameter, text, isUserNode):
        super(NodeItem, self).__init__()
        # Code...

    def mouseDoubleClickEvent(self, event):
        self.nodeDoubleClicked.emit(self.texts)

When trying to run it it give me this error:

line 84, in createUserNodes
newItem.nodeDoubleClicked[str].connect(self.dc)
TypeError: NodeItem cannot be converted to PyQt5.QtCore.QObject in this context

I have no idea what this means or how to fix it.

like image 251
Matthew Lueder Avatar asked Apr 09 '16 21:04

Matthew Lueder


1 Answers

QGraphicsItem does not inherit from QObject, therefore it is not possible to emit a signal from an instance of QGraphicsItem. You can solve this by subclassing QGraphicsObject instead of QGraphicsItem: http://doc.qt.io/qt-5/qgraphicsobject.html.

like image 96
Daniele Pantaleone Avatar answered Oct 13 '22 22:10

Daniele Pantaleone