tags:

views:

108

answers:

1

I've written my own implementation of QGraphicsView.drawItems(), to fit the needs of my application. The method as such works fine, however, it is called repeatedly, even if it does not need to be redrawn. This causes the application to max out processor utilization.
Do I need to somehow signal that the drawing is finished? I read the source in the git tree, and I couldn't see any such thing being done.
The app is in Python/PyQt, and my draw-method looks like this:

def drawItems(self, painter, items, options):
    markupColors={'manual':QColor(0, 0, 255),
                  'automatic':QColor(255, 0, 0),
                  'user':QColor(0, 255, 0)}

    for index in xrange(len(items)):
        item=items[index]
        option=options[index]

        dataAsInt, dataIsInt=item.data(self.DRAWABLE_INDEX).toInt()
        drawable=None
        if dataIsInt:
            drawable=self.drawables[dataAsInt]
            item.setPen(markupColors[drawable.source])
        item.paint(painter, option, None)

The view's method is overridden by "monkey-patching", like this:

    self.ui.imageArea.drawItems=self.drawer.drawItems

The above method is self.drawer.drawItems in the last statement.

Any ideas why this happens?

A: 

I think this causes the problem:

item.setPen(markupColors[drawable.source])

If you take a look at the source code:

void QAbstractGraphicsShapeItem::setPen(const QPen &pen)
{
    Q_D(QAbstractGraphicsShapeItem);
    prepareGeometryChange();
    d->pen = pen;
    d->boundingRect = QRectF();
    update();
}

It calls update() each time pen is set.

wuub
Ah, excellent. Thank you!I changed it to `painter.setPen` instead, but then it does not change color? I'll have to try to set the pen once instead.
carlpett