Dear all,<br><br>I'm facing a problem with my application when<br>a QLinef is superimposed to a color filled QRectf.<br>I would like to always see the whole QLinef but I only get<br>a random behaviour. Sometime, the QLinef appear on top<br>
of the QRectf and sometimes it is hidden by the QRectf.<br><br>An example is shown above, is there something I'm doing wrong?<br><br>Thanks for your help,<br>Marc.<br><br>import sys<br><br>from PyQt4.QtCore import Qt, QLineF, QRectF, SIGNAL<br>
from PyQt4.QtGui import QGraphicsScene, QGridLayout, QHBoxLayout, QGraphicsItem<br>from PyQt4.QtGui import QDialog, QPainter, QGroupBox, QPushButton, QVBoxLayout<br>from PyQt4.QtGui import QApplication, QGraphicsView, QBrush, QColor<br>
<br>class GraphicsView(QGraphicsView):<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 Zone(QGraphicsItem):<br> def __init__(self):<br> super(Zone, self).__init__()<br> self.rect = QRectF(50, 50, 100, 100)<br> self.color = QColor(255,0,0, 255)<br> pass<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>
pass<br><br>class Window(QDialog):<br> def __init__(self, parent=None):<br> self.W = 300<br> self.H = 300<br> super(Window, self).__init__(parent)<br> self.view = GraphicsView()<br> self.scene = QGraphicsScene(self)<br>
self.scene.setSceneRect(0, 0, self.W, self.H)<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> pass<br><br> def createBasicGroup(self):<br> groupBox = QGroupBox('Basic')<br> <br>
drawButton = QPushButton('(Re-)Draw')<br> drawButton.setFocusPolicy(Qt.NoFocus)<br> self.connect(drawButton, SIGNAL("clicked()"), self.setView)<br><br> quitButton = QPushButton('Quit')<br>
quitButton.setFocusPolicy(Qt.NoFocus)<br> self.connect(quitButton, SIGNAL("clicked()"), self.accept)<br><br> vbox = QVBoxLayout()<br> vbox.addWidget(drawButton)<br> vbox.addWidget(quitButton)<br>
vbox.addStretch(1)<br> groupBox.setLayout(vbox)<br> return groupBox<br><br> def setView(self):<br> self.scene.clear()<br> zone = Zone()<br> zone.setZValue(0)<br> self.scene.addItem(zone)<br>
<br> line = QLineF(30, 60, 130, 90)<br> self.scene.addLine(line, Qt.blue)<br> pass<br><br> pass<br><br>app = QApplication(sys.argv)<br>form = Window()<br>rect = QApplication.desktop().availableGeometry()<br>
form.resize(int(rect.width() * 0.5), int(rect.height() * 0.5))<br>form.show()<br>app.exec_()<br><br>