python – PyQt5连接不起作用:在此上下文中无法将项转换为PyQt5.QtCore.QObject

我正在尝试连接来自创建对象的信号并且收到错误.这是我的代码的简化版本:

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)

当试图运行它时,它给我这个错误:

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

我不知道这意味着什么或如何解决它.

最佳答案 QGraphicsItem不从QObject继承,因此无法从QGraphicsItem的实例发出信号.您可以通过继承QGraphicsObject而不是QGraphicsItem:
http://doc.qt.io/qt-5/qgraphicsobject.html来解决此问题.

    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞