[PyQt] Problem when drawing QRectf and QLinef

shi dingan shi.dingan at gmail.com
Thu Mar 10 09:53:59 GMT 2011


Dear all,

I'm facing a problem with my application when
a QLinef is superimposed to a color filled QRectf.
I would like to always see the whole QLinef but I only get
a random behaviour. Sometime, the QLinef appear on top
of the QRectf and sometimes it is hidden by the QRectf.

An example is shown above, is there something I'm doing wrong?

Thanks for your help,
Marc.

import sys

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

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 Zone(QGraphicsItem):
    def __init__(self):
        super(Zone, self).__init__()
        self.rect = QRectF(50, 50, 100, 100)
        self.color = QColor(255,0,0, 255)
        pass

    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
    pass

class Window(QDialog):
    def __init__(self, parent=None):
        self.W = 300
        self.H = 300
        super(Window, self).__init__(parent)
        self.view = GraphicsView()
        self.scene = QGraphicsScene(self)
        self.scene.setSceneRect(0, 0, self.W, self.H)
        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')

        drawButton = QPushButton('(Re-)Draw')
        drawButton.setFocusPolicy(Qt.NoFocus)
        self.connect(drawButton, SIGNAL("clicked()"), self.setView)

        quitButton = QPushButton('Quit')
        quitButton.setFocusPolicy(Qt.NoFocus)
        self.connect(quitButton, SIGNAL("clicked()"), self.accept)

        vbox = QVBoxLayout()
        vbox.addWidget(drawButton)
        vbox.addWidget(quitButton)
        vbox.addStretch(1)
        groupBox.setLayout(vbox)
        return groupBox

    def setView(self):
        self.scene.clear()
        zone = Zone()
        zone.setZValue(0)
        self.scene.addItem(zone)

        line = QLineF(30, 60, 130, 90)
        self.scene.addLine(line, Qt.blue)
        pass

    pass

app = QApplication(sys.argv)
form = Window()
rect = QApplication.desktop().availableGeometry()
form.resize(int(rect.width() * 0.5), int(rect.height() * 0.5))
form.show()
app.exec_()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20110310/982c083d/attachment.html>


More information about the PyQt mailing list