有效绘制和更新数千个QGraphicsRectItem的最佳方法?

时间:2015-03-24 01:38:03

标签: python performance qt graphics qgraphicsview

我是Qt(PySide)的新手,我正在尝试绘制一个网格地图'有效率的。然而,我的解决方案使用10k + QGraphicsRectItem减速停止。

目前它的工作原理如下:

class GridMapView(QObject, QGraphicsItemGroup):

def __init__(self, mapWidth, mapHeight, cellSize):
    QObject.__init__(self)
    QGraphicsItemGroup.__init__(self)

    self.mapWidth = mapWidth
    self.mapHeight = mapHeight
    self.cellSize = cellSize

    self.graphicCells = []

    #Create cells.
    for x in range(self.mapWidth / self.cellSize):
        self.graphicCells.append([])
        for y in range(self.mapHeight / self.cellSize):
            self.graphicCells[x].append(QGraphicsRectItem(x * self.cellSize, y * self.cellSize, self.cellSize, self.cellSize))
            self.graphicCells[x][-1].setBrush(QBrush(QColor('grey')))

            self.addToGroup(self.graphicCells[x][-1])

    self.setPos(-mapWidth/2, -mapHeight/2)


@Slot(Point, int)
def onCellUpdated(self, index, state):
    cell = self.graphicCells[index.x][index.y]
    if state == CellStates.UNKNOWN:
        cell.setBrush(QBrush(QColor('grey')))
        cell.setVisible(True)
    elif state == CellStates.FREE:
        cell.setVisible(False)
    elif state == CellStates.OCCUPIED:
        cell.setBrush(QBrush(QColor('black')))
        cell.setVisible(True)

在创建期间填充初始网格。当触发适当的信号时,将更新特定的单元格。这种更新很少发生,我的假设是Qt只绘制了哪些变化。

整个地图'在我的视口中可见,并且禁用渲染使我的应用程序运行得非常好。

我尝试过设置QGraphicsView.NoViewportUpdate,但它仍然会更新整个视图。我希望它能要求我打电话给.update()'。

这种做法从一开始就有缺陷吗?提前谢谢。

0 个答案:

没有答案
相关问题