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> def __init__(self, graphicsView, parent=None):<br> QtGui.QGraphicsScene.__init__(self, QtCore.QRectF(QtCore.QPointF(0,0), QtCore.QPointF(450,350)), parent)<br> self.itemToAdd = 0<br> self.mode = 0 # 0: add mode, 1:select mode<br>
self.view = graphicsView <br><br> def mousePressEvent(self, mouseEvent):<br> if (mouseEvent.button() != QtCore.Qt.LeftButton):<br> return<br> x = mouseEvent.scenePos().x()<br> y = mouseEvent.scenePos().y()<br>
<br> if(self.mode == 0):<br> self.createItem(x, y) <br> elif(self.mode == 1):<br> self.selectItem(x, y)<br> QtGui.QGraphicsScene.mousePressEvent(self, mouseEvent)<br><br> def createItem(self, x, y):<br>
if (self.itemToAdd == 0):<br> self.addItem(MyPoint(x, y))<br> elif (self.itemToAdd == 1):<br> self.addLine(QtCore.QLineF(x,y,x+50,y+50))<br> self.update()<br># self.view.update()<br># self.view.show()<br>
<br> def selectItem(self, x, y):<br> selected = self.itemAt(x,y)<br> print selected<br> if selected:<br> selected.setSelected(True)<br><br>class StartProgram(QtGui.QMainWindow):<br><br> def __init__(self, parent=None):<br>
QtGui.QWidget.__init__(self, parent)<br> self.ui = Ui_MainWindow()<br> self.ui.setupUi(self)<br> self.scene = DiagramScene(self.ui.graphicsView)<br> self.ui.graphicsView.setScene(self.scene)<br>
self.ui.pointButton.setChecked(True)<br> self.ui.pathButton.setChecked(False)<br><br> QtCore.QObject.connect(self.ui.pointButton,QtCore.SIGNAL("clicked()"), self.pointButtonClicked)<br> QtCore.QObject.connect(self.ui.pathButton,QtCore.SIGNAL("clicked()"), self.pathButtonClicked)<br>
QtCore.QObject.connect(self.ui.selectButton,QtCore.SIGNAL("clicked()"), self.selectButtonClicked)<br><br> def pointButtonClicked(self):<br> self.ui.pointButton.setChecked(True)<br> self.ui.pathButton.setChecked(False)<br>
self.scene.itemToAdd = 0 #0 is point<br><br> def pathButtonClicked(self):<br> self.ui.pathButton.setChecked(True)<br> self.ui.pointButton.setChecked(False)<br> self.scene.itemToAdd = 1 #1 is path<br><br> def selectButtonClicked(self):<br>
if self.ui.selectButton.isChecked():<br> self.scene.mode = 1<br> else :<br> self.scene.mode = 0<br><br>class MyPoint(QtGui.QGraphicsPathItem):<br> pointToDraw = QtCore.QRectF() <br><br> def __init__(self, x, y, parent=None, scene=None):<br>
super(MyPoint, self).__init__()<br> self.x = x<br> self.y = y<br> MyPoint.pointToDraw.setCoords(x,y, x+15,y+15)<br> self.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, True)<br> self.setFlag(QtGui.QGraphicsItem.ItemIsMovable, True)<br>
<br> def paint(self, painter, option, widget=None): <br> if self.isSelected():<br> painter.setBrush(QtGui.QBrush(QtGui.QColor(0,255,255)))<br> else:<br> painter.setBrush(QtGui.QBrush(QtGui.QColor(0,0,255)))<br>
painter.drawRect(MyPoint.pointToDraw)<br><br> def path(self): <br> path = QtGui.QPainterPath()<br> path.addEllipse(MyPoint.pointToDraw) #to add ellipse?!?!?!?!?<br> return path<br><br>if __name__ == "__main__":<br>
app = QtGui.QApplication(sys.argv)<br> myapp = StartProgram()<br> myapp.show()<br> sys.exit(app.exec_())<br><br>__________________________<br><br><br><br>