hi,

I posted that question few days ago, but no answers. Maybe nobody use
QGraphicItem. I do not know. I am new at pyqt (still reading the pyqt
book !).

Please, let me know if there is a better place to talk about
QGraphicsItem, View, Scene, .... I will use it a lot during the next
month. it's a great tool compare to what I was using before.

here was my question,
I use setClipRegion() in the paint() method of a QGraphicsItem.
That Item has got many children. I would like to avoid repeating the
setClipRegion() in all of them.
Thus, I would like to know if it's possible to "propagate" that
Clipping to the children, automatically.

In attachment, a simple .py that show the clipping "not propagating"
from parent to children.
Thanks for your help!
#!/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_()

_______________________________________________
PyQt mailing list    [email protected]
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to