Hi,<br>I am trying to implement a simple paint like program. I am having mainly two problems:<br><br>1. I have an item class inherited from QGraphicsItem and I use QGraphicsScene.addItem() to add it to the scene. However, I can only see the last added item. Although all previous items (when I use QGraphicsScene.items()) are present and their visible properties are true, they are not shown on my scene. This thing only happens when I use addItem() with the class I implemented. If I use addLine() more than once, I can see all items created by that addLine() function.<br>
<br>2.The other problem is when I try to select an item with a left mouse click. I did not implement the mousePressEvent() inside the item class but I implemented the one in scene class. I can get the mouse scenePos() easily and use it to add items. After adding an item, if I use itemAt() function with a new click on the item and scenePos(), it always returns None. I set the flags of the item all true. <br>
<br>I am afraid these two problems are related but I could not find a solution.<br><br>Thanks<br><br>Aysun Bascetincelik<br><br><br>
Here is the code:<br>__________________________<br><br><br>#!/usr/bin/env python<br><br>import sys<br>from PyQt4 import QtCore, QtGui<br>from graphicsForm import Ui_MainWindow<br><br>class DiagramScene(QtGui.QGraphicsScene):<br>
<br>&nbsp;&nbsp;&nbsp; def __init__(self, graphicsView, parent=None):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; QtGui.QGraphicsScene.__init__(self, QtCore.QRectF(QtCore.QPointF(0,0), QtCore.QPointF(450,350)), parent)<br>&nbsp;&nbsp;&nbsp; self.itemToAdd = 0<br>&nbsp;&nbsp;&nbsp; self.mode = 0 # 0: add mode, 1:select mode<br>
&nbsp;&nbsp;&nbsp; self.view = graphicsView&nbsp;&nbsp;&nbsp; <br><br>&nbsp;&nbsp;&nbsp; def mousePressEvent(self, mouseEvent):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (mouseEvent.button() != QtCore.Qt.LeftButton):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return<br>&nbsp;&nbsp;&nbsp; x = mouseEvent.scenePos().x()<br>&nbsp;&nbsp;&nbsp; y = mouseEvent.scenePos().y()<br>
&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; if(self.mode == 0):<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; self.createItem(x, y) <br>&nbsp;&nbsp;&nbsp; elif(self.mode == 1):<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; self.selectItem(x, y)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; QtGui.QGraphicsScene.mousePressEvent(self, mouseEvent)<br><br>&nbsp;&nbsp;&nbsp; def createItem(self, x, y):<br>
&nbsp;&nbsp;&nbsp; if (self.itemToAdd == 0):<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; self.addItem(MyPoint(x, y))<br>&nbsp;&nbsp;&nbsp; elif (self.itemToAdd == 1):<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; self.addLine(QtCore.QLineF(x,y,x+50,y+50))<br>&nbsp;&nbsp;&nbsp; self.update()<br>#&nbsp;&nbsp;&nbsp; self.view.update()<br>#&nbsp;&nbsp;&nbsp; self.view.show()<br>
<br>&nbsp;&nbsp;&nbsp; def selectItem(self, x, y):<br>&nbsp;&nbsp;&nbsp; selected = self.itemAt(x,y)<br>&nbsp;&nbsp;&nbsp; print selected<br>&nbsp;&nbsp;&nbsp; if selected:<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; selected.setSelected(True)<br><br>class StartProgram(QtGui.QMainWindow):<br><br>&nbsp;&nbsp;&nbsp; def __init__(self, parent=None):<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; QtGui.QWidget.__init__(self, parent)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.ui = Ui_MainWindow()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.ui.setupUi(self)<br>&nbsp;&nbsp;&nbsp; self.scene = DiagramScene(self.ui.graphicsView)<br>&nbsp;&nbsp;&nbsp; self.ui.graphicsView.setScene(self.scene)<br>
&nbsp;&nbsp;&nbsp; self.ui.pointButton.setChecked(True)<br>&nbsp;&nbsp;&nbsp; self.ui.pathButton.setChecked(False)<br><br>&nbsp;&nbsp;&nbsp; QtCore.QObject.connect(self.ui.pointButton,QtCore.SIGNAL(&quot;clicked()&quot;), self.pointButtonClicked)<br>&nbsp;&nbsp;&nbsp; QtCore.QObject.connect(self.ui.pathButton,QtCore.SIGNAL(&quot;clicked()&quot;), self.pathButtonClicked)<br>
&nbsp;&nbsp;&nbsp; QtCore.QObject.connect(self.ui.selectButton,QtCore.SIGNAL(&quot;clicked()&quot;), self.selectButtonClicked)<br><br>&nbsp;&nbsp;&nbsp; def pointButtonClicked(self):<br>&nbsp;&nbsp;&nbsp; self.ui.pointButton.setChecked(True)<br>&nbsp;&nbsp;&nbsp; self.ui.pathButton.setChecked(False)<br>
&nbsp;&nbsp;&nbsp; self.scene.itemToAdd = 0 #0 is point<br><br>&nbsp;&nbsp;&nbsp; def pathButtonClicked(self):<br>&nbsp;&nbsp;&nbsp; self.ui.pathButton.setChecked(True)<br>&nbsp;&nbsp;&nbsp; self.ui.pointButton.setChecked(False)<br>&nbsp;&nbsp;&nbsp; self.scene.itemToAdd = 1 #1 is path<br><br>&nbsp;&nbsp;&nbsp; def selectButtonClicked(self):<br>
&nbsp;&nbsp;&nbsp; if self.ui.selectButton.isChecked():<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; self.scene.mode = 1<br>&nbsp;&nbsp;&nbsp; else :<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; self.scene.mode = 0<br><br>class MyPoint(QtGui.QGraphicsPathItem):<br>&nbsp;&nbsp;&nbsp; pointToDraw = QtCore.QRectF()&nbsp;&nbsp;&nbsp; <br><br>&nbsp;&nbsp;&nbsp; def __init__(self, x, y, parent=None, scene=None):<br>
&nbsp; &nbsp;&nbsp;&nbsp; super(MyPoint, self).__init__()<br>&nbsp;&nbsp;&nbsp; self.x = x<br>&nbsp;&nbsp;&nbsp; self.y = y<br>&nbsp;&nbsp;&nbsp; MyPoint.pointToDraw.setCoords(x,y, x+15,y+15)<br>&nbsp;&nbsp;&nbsp; self.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, True)<br>&nbsp;&nbsp;&nbsp; self.setFlag(QtGui.QGraphicsItem.ItemIsMovable, True)<br>
<br>&nbsp;&nbsp;&nbsp; def paint(self, painter, option, widget=None):&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; if self.isSelected():<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; painter.setBrush(QtGui.QBrush(QtGui.QColor(0,255,255)))<br>&nbsp;&nbsp;&nbsp; else:<br>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; painter.setBrush(QtGui.QBrush(QtGui.QColor(0,0,255)))<br>
&nbsp;&nbsp;&nbsp; painter.drawRect(MyPoint.pointToDraw)<br><br>&nbsp;&nbsp;&nbsp; def path(self):&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; path = QtGui.QPainterPath()<br>&nbsp;&nbsp;&nbsp; path.addEllipse(MyPoint.pointToDraw) #to add ellipse?!?!?!?!?<br>&nbsp;&nbsp;&nbsp; return path<br><br>if __name__ == &quot;__main__&quot;:<br>
&nbsp;&nbsp;&nbsp; app = QtGui.QApplication(sys.argv)<br>&nbsp;&nbsp;&nbsp; myapp = StartProgram()<br>&nbsp;&nbsp;&nbsp; myapp.show()<br>&nbsp;&nbsp;&nbsp; sys.exit(app.exec_())<br><br>__________________________<br><br><br><br>