[PyQt] Problem with colored rectangles

shi dingan shi.dingan at gmail.com
Sat Feb 19 22:22:10 GMT 2011


Hello,

I have a problem with my code when I try to draw two rectangles with
overlap.
I draw a red one, then a green with a small part of it overlapping the red
rectangle.
But when I refresh the scene, I have a random behavior, that is, sometimes
only the
red rectangle appears and sometimes both rectangle appear.
Is it a normal behavior?
I was expecting to see always both rectangle since the
red one is always the first one added to the scene...

Here is the code:
import sys

from PyQt4.QtCore import QRectF, SIGNAL, Qt, QFileInfo
from PyQt4.QtGui import QGraphicsItem, QGraphicsView, QDialog, QApplication
from PyQt4.QtGui import QPainter, QGraphicsScene, QGridLayout, QHBoxLayout
from PyQt4.QtGui import QVBoxLayout, QColor, QGroupBox, QPushButton
from PyQt4.QtGui import QFileDialog, QBrush

class Zone(QGraphicsItem):
    def __init__(self, x1, y1, x2, y2, color):
        super(Zone, self).__init__()
        self.rect = QRectF(x1, y1, x2 - x1, y2 - y1)
        self.color = color

    def boundingRect(self):
        return self.rect

    def paint(self, painter, option, widget=None):
        painter.setPen(Qt.NoPen)
        painter.setBrush(QBrush(self.color))
        painter.drawRect(self.rect)
    pass

class GraphicsView(QGraphicsView):

    def __init__(self, parent=None):
        super(GraphicsView, self).__init__(parent)
        self.setDragMode(QGraphicsView.ScrollHandDrag)
        self.setRenderHint(QPainter.Antialiasing)
        self.setRenderHint(QPainter.TextAntialiasing)
        pass

    def wheelEvent(self, event):
        factor = 1.41 ** (-event.delta() / 240.0)
        self.scale(factor, factor)
        pass
    pass

class Window(QDialog):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        self.view = GraphicsView()
        self.scene = QGraphicsScene(self)
        self.scene.setSceneRect(0, 0, 100, 100)
        self.view.setScene(self.scene)

        grid = QGridLayout()
        grid.addWidget(self.createBasicGroup(), 0, 0)
        layout = QHBoxLayout()
        layout.addWidget(self.view, 1)
        layout.addLayout(grid)
        self.setLayout(layout)

        pass

    def createBasicGroup(self):
        groupBox = QGroupBox('Basic')

        refreshButton = QPushButton('Refresh')
        refreshButton.setFocusPolicy(Qt.NoFocus)
        self.connect(refreshButton, SIGNAL("clicked()"), self.refresh)

        vbox = QVBoxLayout()
        vbox.addWidget(refreshButton)
        vbox.addStretch(1)
        groupBox.setLayout(vbox)

        return groupBox

    def refresh(self):
        self.scene.clear()
        color1 = QColor(255, 0, 0, 255)
        zone1 = Zone(0, 0, 50, 50, color1)
        self.scene.addItem(zone1)
        color2 = QColor(0, 255, 0, 255)
        zone2 = Zone(20, 10, 50, 50, color2)
        self.scene.addItem(zone2)
        pass

app = QApplication(sys.argv)
form = Window()
rect = QApplication.desktop().availableGeometry()
form.resize(int(rect.width() * 0.6), int(rect.height() * 0.9))
form.show()
app.exec_()

If it is a normal behavior, is there a way to draw the rectangle always in
the
same order?

Thanks in advance,
Marc.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20110219/ad8e5a7d/attachment.html>


More information about the PyQt mailing list