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

旧街凉风 提交于 2019-12-19 03:40:10

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/36522809/pyqt5-connection-doesnt-work-item-cannot-be-converted-to-pyqt5-qtcore-qobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!