<div dir="ltr"><div>Hello,</div><div><br></div><div>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:</div><div><br></div><div><div>import sys</div><div>from PyQt5.QtCore import Qt, pyqtSignal</div><div>from PyQt5.QtGui import QPen</div><div>from PyQt5.QtWidgets import (QApplication, QGraphicsTextItem,</div><div> QGraphicsRectItem, QGraphicsScene, QGraphicsView)</div><div><br></div><div><br></div><div>class TextItem(QGraphicsTextItem):</div><div> selectedChange = pyqtSignal(str)</div><div><br></div><div> def __init__(self, parent=None, scene=None):</div><div> super(TextItem, self).__init__(parent, scene)</div><div> self.setPlainText('test2')</div><div><br></div><div> def mousePressEvent(self, event):</div><div> self.selectedChange.emit('TextItem')</div><div> super(TextItem, self).mousePressEvent(event)</div><div><br></div><div><br></div><div>class RectItem(QGraphicsRectItem):</div><div> selectedChange = pyqtSignal(str)</div><div><br></div><div> def __init__(self, parent=None):</div><div> super(RectItem, self).__init__(-30, -30, 30, 30, parent)</div><div> pen = QPen(Qt.red, 2, Qt.SolidLine,</div><div> Qt.RoundCap, Qt.RoundJoin)</div><div> self.setPen(pen)</div><div><br></div><div> def mousePressEvent(self, event):</div><div> self.selectedChange.emit('RectItem')</div><div> super(RectItem, self).mousePressEvent(event)</div><div><br></div><div><br></div><div>def message(string):</div><div> print(string)</div><div><br></div><div><br></div><div>app = QApplication(sys.argv)</div><div>scene = QGraphicsScene()</div><div><br></div><div>text = TextItem()</div><div>text.selectedChange.connect(message)</div><div>scene.addItem(text)</div><div><br></div><div>rect = RectItem()</div><div>rect.selectedChange.connect(message)</div><div>scene.addItem(rect)</div><div><br></div><div>view = QGraphicsView(scene)</div><div>view.show()</div><div><br></div><div>sys.exit(app.exec_())</div></div><div><br></div><div><br></div><div>And I got error:</div><div><div>test3.py, line 48, in <module></div><div> rect.selectedChange.connect(message)</div><div>TypeError: RectItem cannot be converted to PyQt5.QtCore.QObject in this context</div></div><div><br></div>How to solve this?<div><br clear="all"><div><div class="gmail_signature">Zdenko</div></div>
<input name="virtru-metadata" type="hidden" value="{"email-policy":{"state":"closed","expirationUnit":"days","disableCopyPaste":false,"disablePrint":false,"disableForwarding":false,"expires":false},"attachments":{}}"></div></div>