[PyQt] [pyqt] how to propagate a painter.setClipRegion() to children of a QGraphicsItem
Philippe Crave
philippe.crave at gmail.com
Fri Apr 2 18:05:32 BST 2010
Hello,
For few days, I try to propagate a painter.setClipRegion() to the
children of a QGraphicsItem.
I hope someone can help me little bit.
I create 2 items. r1 and r2. r2's parent is r1.
I add r1 to the scene.
r1 has got a setClipRegion in is paint() method.
the clipping works perfect on r1, but not on r2 :/
here is my code..
- - - - - - - - - - - - - - - - - - - -
#!/usr/bin/env python
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class BoxItem(QGraphicsItem):
def __init__(self, position, color, rect):
super(BoxItem, self).__init__()
self.setFlags(QGraphicsItem.ItemIsSelectable|
QGraphicsItem.ItemIsMovable|
QGraphicsItem.ItemIsFocusable)
self.rect = rect
self.color = color
self.setPos(position)
self.setMatrix(QMatrix())
def boundingRect(self):
return self.rect
def paint(self, painter, option, widget):
painter.setClipRegion(QRegion(QRect(0, 0, 20, 20)))
painter.setPen(Qt.NoPen)
painter.setBrush(QBrush(self.color))
painter.drawRect(self.rect)
class IntBoxItem(QGraphicsItem):
def __init__(self, position, color, rect, parent):
super(IntBoxItem, self).__init__(parent)
self.setFlags(QGraphicsItem.ItemIsSelectable|
QGraphicsItem.ItemIsMovable|
QGraphicsItem.ItemIsFocusable)
self.rect = rect
self.color = color
self.setPos(position)
self.setMatrix(QMatrix())
self.setOpacity(0.7)
def boundingRect(self):
return self.rect
def paint(self, painter, option, widget):
painter.setPen(Qt.NoPen)
painter.setBrush(QBrush(self.color))
painter.drawRect(self.rect)
class MainForm(QDialog):
def __init__(self, parent=None):
super(MainForm, self).__init__(parent)
self.scene = QGraphicsScene(self)
self.scene.setSceneRect(0, 0, 200, 200)
self.view = QGraphicsView()
self.view.setScene(self.scene)
layout = QVBoxLayout()
layout.addWidget(self.view)
self.setLayout(layout)
self.r1 = BoxItem(QPointF(0, 0), QColor(255, 0, 0), QRectF(0,
0, 100, 100))
self.r2 = IntBoxItem(QPointF(0, 0), QColor(255, 255, 0),
QRectF(0, 0, 50, 50), self.r1)
self.scene.addItem(self.r1)
app = QApplication(sys.argv)
form = MainForm()
form.show()
app.exec_()
More information about the PyQt
mailing list