[PyQt] How to emit signal from QGraphicsRectItem

zdenko podobny zdenop at gmail.com
Thu Sep 3 21:32:07 BST 2015


Hello,

I found out that I am not able to emit signal from class derived from
QGraphicsRectItem (while emiting signal works when I use QGraphicsTextItem
;-) ). Here is my test case:

import sys
from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtGui import QPen
from PyQt5.QtWidgets import (QApplication, QGraphicsTextItem,
                             QGraphicsRectItem, QGraphicsScene,
QGraphicsView)


class TextItem(QGraphicsTextItem):
    selectedChange = pyqtSignal(str)

    def __init__(self, parent=None, scene=None):
        super(TextItem, self).__init__(parent, scene)
        self.setPlainText('test2')

    def mousePressEvent(self, event):
        self.selectedChange.emit('TextItem')
        super(TextItem, self).mousePressEvent(event)


class RectItem(QGraphicsRectItem):
    selectedChange = pyqtSignal(str)

    def __init__(self, parent=None):
        super(RectItem, self).__init__(-30, -30, 30, 30, parent)
        pen = QPen(Qt.red, 2, Qt.SolidLine,
                   Qt.RoundCap, Qt.RoundJoin)
        self.setPen(pen)

    def mousePressEvent(self, event):
        self.selectedChange.emit('RectItem')
        super(RectItem, self).mousePressEvent(event)


def message(string):
    print(string)


app = QApplication(sys.argv)
scene = QGraphicsScene()

text = TextItem()
text.selectedChange.connect(message)
scene.addItem(text)

rect = RectItem()
rect.selectedChange.connect(message)
scene.addItem(rect)

view = QGraphicsView(scene)
view.show()

sys.exit(app.exec_())


And I got error:
test3.py, line 48, in <module>
    rect.selectedChange.connect(message)
TypeError: RectItem cannot be converted to PyQt5.QtCore.QObject in this
context

How to solve this?

Zdenko
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20150903/3f69c79e/attachment.html>


More information about the PyQt mailing list