Custom QAbstractGraphicsShapeItem very slow in Qt Jambi
Posted
by Mene
on Stack Overflow
See other posts from Stack Overflow
or by Mene
Published on 2010-06-11T01:20:56Z
Indexed on
2010/06/11
1:42 UTC
Read the original article
Hit count: 347
I try to implement an own QAbstractGraphicsShapeItem in Qt Jambi (4.5) but as soon as I implement a custom class (rather than use e.g. QGraphicsEllipseItem) the rendering speed drops by about on order of magnitude. If looked in the original Code of QGraphicsEllipseItem and it seems to do basicly the same.
class Circle extends QAbstractGraphicsShapeItem {
final private QRectF rect = new QRectF();
public final void setRect(double x, double y, double w,
double h) {
QRectF newRect = new QRectF(x, y, w, h);
if (!newRect.equals(rect)) {
prepareGeometryChange();
this.rect.setX(x);
this.rect.setY(y);
this.rect.setWidth(w);
this.rect.setHeight(h);
this.update(rect);
}
}
@Override
public final QRectF boundingRect() {
return rect;
}
QPainterPath shape;
@Override
public final QPainterPath shape() {
if (shape == null) {
QPainterPath path = new QPainterPath();
path.addEllipse(rect);
QPainterPathStroker stroker = new QPainterPathStroker();
QPen pen = this.pen();
stroker.setCapStyle(pen.capStyle());
stroker.setWidth(pen.widthF());
shape = stroker.createStroke(path);
}
return shape;
}
@Override
public final void paint(QPainter painter,
QStyleOptionGraphicsItem option, QWidget widget) {
painter.setPen(this.pen());
painter.setBrush(this.brush());
painter.drawEllipse(this.rect);
}
}
I need to render a lot of objects and they are mostly moving, so speed is essential. Anyone knows how to speed this up? I underestand that Java might be slower then the nativ code in the library. But the code in the paint method seems not to be the problem, since commenting it out doesn't change the speed notably.
© Stack Overflow or respective owner