Skip to content

Is there any way to draw great number of rectangles in the viewbox? #2287

@earphonebreaker

Description

@earphonebreaker

I made a graphic viewer by using pyqtgraph. The code logic is like this:

Graphic Viewer

pg = GraphicsLayoutWidget()
vb = pg.addViewBox()
vb.addItem(RectItem)

The RectItem is a Class that I copied and modified from other's code:

class RectItem(pg.GraphicsObject):
    def __init__(self, rect,line,brush, parent=None,line_width=1):
        super().__init__(parent)
        self._rect = rect
        self.picture = QtGui.QPicture()
        self._generate_picture(line,brush,line_width)

    def _generate_picture(self,line,brush,line_width):
        painter = QtGui.QPainter(self.picture)
        painter.setPen(pg.mkPen(line,width=line_width))
        painter.setBrush(pg.mkBrush(brush))
        painter.drawRect(self._rect)
        painter.end()

    def paint(self, painter, option, widget=None):
        painter.drawPicture(0, 0, self.picture)

    def boundingRect(self):
        return QtCore.QRectF(self.picture.boundingRect())

My problem is, when there are only several RectItem added into the viewbox, the program works perfectly fine, but when great numbers of RectItem(i.e. more than 100000) are added, the program will be extremely slow and even crash. I also tried to draw them in one item by modifying the RectItem class into this:

class GraphLayerItem(pg.GraphicsObject):
    def __init__(self, rect_list, line,brush,parent=None):
        super().__init__(parent)
        self.rect_list = rect_list
        self.line = line
        self.brush = brush
        self.picture = QtGui.QPicture()
        self._generate_picture()


    def _generate_picture(self):
        painter = QtGui.QPainter(self.picture)
        painter.setPen(pg.mkPen(self.line))
        painter.setBrush(pg.mkBrush(self.brush))
        for rect in self.rect_list:
            painter.drawRect(rect)
        painter.end()

    def paint(self, painter, option, widget=None):
        painter.drawPicture(0, 0, self.picture)

    def boundingRect(self):
        return QtCore.QRectF(self.picture.boundingRect())

However, this doesn't help either when the rect_list is very large. Is there any way to change the display options, such as changing dpi while zooming in and out, or making a filter when there are too many items displayed in current viewbox range? Or any other ways to fix this problem? Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions