python - PyQt5 连接在这种情况下不起作用 : item cannot be converted to PyQt5. QtCore.QObject

标签 python qt pyqt signals-slots pyqt5

我正在尝试连接来自已创建对象的信号,但出现错误。这是我的代码的简化版本:

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 .

关于python - PyQt5 连接在这种情况下不起作用 : item cannot be converted to PyQt5. QtCore.QObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36522809/

相关文章:

Python:未检测到已安装的 Selenium 包

python - 通过 TCP 连接 Unity 模拟与 python Controller

python - 在 PyGTK/GtkBuilder 中处理多个窗口

python - 如何在 PyQt 中获取 qtablewidget 的选定行的值?

python - 如何在布局中设置正确的列大小 - PySide/PyQt

python - 如何在 readthedocs 上正确设置 PyQt5 导入?

python - 如何相邻显示两个分布图?

Qt、VS2005、Qwt - 无法在从 Qwt 小部件派生的类中使用 Q_OBJECT

c++ - qextserialport 在 Windows 上丢失数据 — 我该怎么办?

c++ - 不推荐包含来自 include/Qt 的头文件