[PyQt] Newbie - QGraphicsItem & QGraphicsScene problems
Aysun Bascetincelik
aysunbcc at gmail.com
Tue May 13 18:25:59 BST 2008
Hi,
I am trying to implement a simple paint like program. I am having mainly two
problems:
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.
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.
I am afraid these two problems are related but I could not find a solution.
Thanks
Aysun Bascetincelik
Here is the code:
__________________________
#!/usr/bin/env python
import sys
from PyQt4 import QtCore, QtGui
from graphicsForm import Ui_MainWindow
class DiagramScene(QtGui.QGraphicsScene):
def __init__(self, graphicsView, parent=None):
QtGui.QGraphicsScene.__init__(self,
QtCore.QRectF(QtCore.QPointF(0,0), QtCore.QPointF(450,350)), parent)
self.itemToAdd = 0
self.mode = 0 # 0: add mode, 1:select mode
self.view = graphicsView
def mousePressEvent(self, mouseEvent):
if (mouseEvent.button() != QtCore.Qt.LeftButton):
return
x = mouseEvent.scenePos().x()
y = mouseEvent.scenePos().y()
if(self.mode == 0):
self.createItem(x, y)
elif(self.mode == 1):
self.selectItem(x, y)
QtGui.QGraphicsScene.mousePressEvent(self, mouseEvent)
def createItem(self, x, y):
if (self.itemToAdd == 0):
self.addItem(MyPoint(x, y))
elif (self.itemToAdd == 1):
self.addLine(QtCore.QLineF(x,y,x+50,y+50))
self.update()
# self.view.update()
# self.view.show()
def selectItem(self, x, y):
selected = self.itemAt(x,y)
print selected
if selected:
selected.setSelected(True)
class StartProgram(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.scene = DiagramScene(self.ui.graphicsView)
self.ui.graphicsView.setScene(self.scene)
self.ui.pointButton.setChecked(True)
self.ui.pathButton.setChecked(False)
QtCore.QObject.connect(self.ui.pointButton,QtCore.SIGNAL("clicked()"),
self.pointButtonClicked)
QtCore.QObject.connect(self.ui.pathButton,QtCore.SIGNAL("clicked()"),
self.pathButtonClicked)
QtCore.QObject.connect(self.ui.selectButton,QtCore.SIGNAL("clicked()"),
self.selectButtonClicked)
def pointButtonClicked(self):
self.ui.pointButton.setChecked(True)
self.ui.pathButton.setChecked(False)
self.scene.itemToAdd = 0 #0 is point
def pathButtonClicked(self):
self.ui.pathButton.setChecked(True)
self.ui.pointButton.setChecked(False)
self.scene.itemToAdd = 1 #1 is path
def selectButtonClicked(self):
if self.ui.selectButton.isChecked():
self.scene.mode = 1
else :
self.scene.mode = 0
class MyPoint(QtGui.QGraphicsPathItem):
pointToDraw = QtCore.QRectF()
def __init__(self, x, y, parent=None, scene=None):
super(MyPoint, self).__init__()
self.x = x
self.y = y
MyPoint.pointToDraw.setCoords(x,y, x+15,y+15)
self.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, True)
self.setFlag(QtGui.QGraphicsItem.ItemIsMovable, True)
def paint(self, painter, option, widget=None):
if self.isSelected():
painter.setBrush(QtGui.QBrush(QtGui.QColor(0,255,255)))
else:
painter.setBrush(QtGui.QBrush(QtGui.QColor(0,0,255)))
painter.drawRect(MyPoint.pointToDraw)
def path(self):
path = QtGui.QPainterPath()
path.addEllipse(MyPoint.pointToDraw) #to add ellipse?!?!?!?!?
return path
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = StartProgram()
myapp.show()
sys.exit(app.exec_())
__________________________
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20080513/d11fd40b/attachment.html
More information about the PyQt
mailing list