[PyQt] strange bug re graphics scene in PyQt 5.7

oliver oliver.schoenborn at gmail.com
Thu Sep 8 18:40:19 BST 2016


Has anyone tried running the Python code? Anyone replicated the problem on
their system?

It would be good to know if it is a Qt bug or PyQt bug. I don't have a C++
dev env here but it would be pretty close to the following, any kind soul
available out there who could compile and run this and see if the same
issue? then I could raise a ticket in Qt bug tracker.

#include <QApplication>
#include <QtCore>
#include <QtWidgets>
#include <QtGui>


class MyGraphicsItem: public QGraphicsObject
{
public:
    MyGraphicsItem::MyGraphicsItem()
    {
        default_brush = new QBrush(QColor(15, 150, 150), Qt.SolidPattern);

        widget_rect = new QGraphicsRectItem(this);
        widget_rect->setPen(new QPen(Qt::NoPen));

        QLabel* widget = new QLabel("hello");
        part_item = new QGraphicsProxyWidget(this);
        part_item->setWidget(widget);

        float pos_x = 33*25*, pos_y = -10;
        setPos(QPointF(pos_x, pos_y));

        widget_rect->setRect(part_item->boundingRect());

        background_path = new QPainterPath();
        QPolygonF trapezoid_poly(widget_rect->boundingRect().adjusted(-10,
-10, 10, 10));
        background_path->addPolygon(trapezoid_poly);
        background_path->closeSubpath();
    }

    QRectF boundingRect() const Q_DECL_OVERRIDE { return
background_path->boundingRect(); }

    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget = 0) Q_DECL_OVERRIDE
    {
        painter->setPen(Qt.NoPen);
        painter->setBrush(__default_brush);
        painter->drawPath(__background_path);
    }

private:
    QBrush* default_brush;
    QGraphicsRectItem* widget_rect;
    QGraphicsProxyWidget* part_item;
    QPainterPath* background_path;

};


class MyScene: public QGraphicsScene
{
public:
    MyScene() {
        for (i=0; i<10; i++)
            addItem(new MyGraphicsItem());
    }
};


int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

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

    QGraphicsView* view = new QGraphicsView();
    MyScene* scene = new MyScene();
    view->setScene(scene);

    layout->addWidget(new QTextEdit());
    layout->addWidget(view);

    widget.show();

    return app.exec();
}


Best,
Oliver

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


On Tue, Sep 6, 2016 at 8:14 PM, oliver <oliver.schoenborn at gmail.com> wrote:

> 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/20160908/cb24e19f/attachment-0001.html>


More information about the PyQt mailing list