Hello,<br><br>I have a problem with my code when I try to draw two rectangles with overlap.<br>I draw a red one, then a green with a small part of it overlapping the red rectangle.<br>But when I refresh the scene, I have a random behavior, that is, sometimes only the<br>
red rectangle appears and sometimes both rectangle appear.<br>Is it a normal behavior?<br>I was expecting to see always both rectangle since the<br>red one is always the first one added to the scene...<br><br>Here is the code:<br>
import sys<br><br>from PyQt4.QtCore import QRectF, SIGNAL, Qt, QFileInfo<br>from PyQt4.QtGui import QGraphicsItem, QGraphicsView, QDialog, QApplication<br>from PyQt4.QtGui import QPainter, QGraphicsScene, QGridLayout, QHBoxLayout<br>
from PyQt4.QtGui import QVBoxLayout, QColor, QGroupBox, QPushButton<br>from PyQt4.QtGui import QFileDialog, QBrush<br><br>class Zone(QGraphicsItem):<br> def __init__(self, x1, y1, x2, y2, color):<br> super(Zone, self).__init__()<br>
self.rect = QRectF(x1, y1, x2 - x1, y2 - y1)<br> self.color = color<br> <br> def boundingRect(self):<br> return self.rect<br><br> def paint(self, painter, option, widget=None):<br> painter.setPen(Qt.NoPen)<br>
painter.setBrush(QBrush(self.color))<br> painter.drawRect(self.rect)<br> pass<br><br>class GraphicsView(QGraphicsView):<br><br> def __init__(self, parent=None):<br> super(GraphicsView, self).__init__(parent)<br>
self.setDragMode(QGraphicsView.ScrollHandDrag)<br> self.setRenderHint(QPainter.Antialiasing)<br> self.setRenderHint(QPainter.TextAntialiasing)<br> pass<br><br> def wheelEvent(self, event):<br>
factor = 1.41 ** (-event.delta() / 240.0)<br> self.scale(factor, factor)<br> pass<br> pass<br><br>class Window(QDialog):<br> def __init__(self, parent=None):<br> super(Window, self).__init__(parent)<br>
<br> self.view = GraphicsView()<br> self.scene = QGraphicsScene(self)<br> self.scene.setSceneRect(0, 0, 100, 100)<br> self.view.setScene(self.scene)<br><br> grid = QGridLayout()<br> grid.addWidget(self.createBasicGroup(), 0, 0)<br>
layout = QHBoxLayout()<br> layout.addWidget(self.view, 1)<br> layout.addLayout(grid)<br> self.setLayout(layout)<br><br> pass<br><br> def createBasicGroup(self):<br> groupBox = QGroupBox('Basic')<br>
<br> refreshButton = QPushButton('Refresh')<br> refreshButton.setFocusPolicy(Qt.NoFocus)<br> self.connect(refreshButton, SIGNAL("clicked()"), self.refresh)<br><br> vbox = QVBoxLayout()<br>
vbox.addWidget(refreshButton)<br> vbox.addStretch(1)<br> groupBox.setLayout(vbox)<br><br> return groupBox<br><br> def refresh(self):<br> self.scene.clear()<br> color1 = QColor(255, 0, 0, 255)<br>
zone1 = Zone(0, 0, 50, 50, color1)<br> self.scene.addItem(zone1)<br> color2 = QColor(0, 255, 0, 255)<br> zone2 = Zone(20, 10, 50, 50, color2)<br> self.scene.addItem(zone2)<br> pass<br>
<br>app = QApplication(sys.argv)<br>form = Window()<br>rect = QApplication.desktop().availableGeometry()<br>form.resize(int(rect.width() * 0.6), int(rect.height() * 0.9))<br>form.show()<br>app.exec_()<br><br>If it is a normal behavior, is there a way to draw the rectangle always in the<br>
same order?<br><br>Thanks in advance,<br>Marc.<br>