[PyQt] strange bug re graphics scene in PyQt 5.7

oliver oliver.schoenborn at gmail.com
Wed Sep 7 01:14:34 BST 2016


We just upgraded to PyQt 5.7 and this is the last problem left to fix, here
is a standalone example that I created from our application code:

   1. Run it;
   2. When the window appears, scroll to the left until you see some
   rectangles, but scroll such that you see only about half the rectangle (so
   the other half is outside the viewport).
   3. Then click in the text box to the left of the graphics view.
   4. The clipped rectangle gets drawn over the text box!!!

This did not occur in 5.5.1. It seems it has something to do with drawing
the background path, but I can't see what we are doing wrong.


from random import random

from PyQt5.QtCore import Qt, QRectF, QPointF
from PyQt5.QtWidgets import QApplication, QGraphicsView, QHBoxLayout,
QTextEdit, QGraphicsScene
from PyQt5.QtWidgets import QLabel, QWidget, QGraphicsProxyWidget,
QGraphicsObject
from PyQt5.QtWidgets import QStyleOptionGraphicsItem, QGraphicsRectItem
from PyQt5.QtGui import QColor, QBrush, QPainter, QPainterPath, QPen,
QPolygonF


class MyGraphicsItem(QGraphicsObject):

    def __init__(self):
        QGraphicsObject.__init__(self)

        self.__default_brush = QBrush(QColor(15, 150, 150), Qt.SolidPattern)

        self.__widget_rect = QGraphicsRectItem(self)
        self.__widget_rect.setPen(QPen(Qt.NoPen))

        widget = QLabel('hello')
        self.__part_item = QGraphicsProxyWidget(self)
        self.__part_item.setWidget(widget)

        pos_x, pos_y = 33*25*(1 if random() > 0.5 else -1), -10*random()
        self.setPos(QPointF(pos_x, pos_y))

        widget_rect = QRectF(self.__part_item.boundingRect())
        self.__widget_rect.setRect(widget_rect)

        self.__background_path = QPainterPath()
        trapezoid_poly =
QPolygonF(self.__widget_rect.boundingRect().adjusted(-10, -10, 10, 10))
        self.__background_path.addPolygon(trapezoid_poly)
        self.__background_path.closeSubpath()

    def paint(self, painter: QPainter, option: QStyleOptionGraphicsItem,
widget: QWidget=None):
        painter.setPen(Qt.NoPen)
        painter.setBrush(self.__default_brush)
        painter.drawPath(self.__background_path)

    def boundingRect(self) -> QRectF:
        return self.__background_path.boundingRect()


class MyScene(QGraphicsScene):
    def __init__(self):
        QGraphicsScene.__init__(self)
        for i in range(10):
            self.addItem(MyGraphicsItem())


app = QApplication([])

widget = QWidget()
layout = QHBoxLayout()
widget.setLayout(layout)

view = QGraphicsView()
scene = MyScene()
view.setScene(scene)

layout.addWidget(QTextEdit())
layout.addWidget(view)

widget.show()
app.exec()



Cheers,
Oliver
Open Source contributions: PyPubSub <http://pubsub.sf.net/>, nose2pytest
<https://github.com/schollii/nose2pytest>, Lua-iCxx
<http://lua-icxx.sf.net/>, iof <http://iof.sf.net/>
StackOverflow <http://stackoverflow.com/users/869951/schollii> contributions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20160906/73e63c67/attachment.html>


More information about the PyQt mailing list