PDA

View Full Version : QGraphicsScene too slow



samsam
10th July 2009, 12:29
Hello there

I am trying to visualize a cartesian 2D-plane with x/y axis and about one milion dots (QRectF).



void myWidget::setupView()
{
scene.setSceneRect(0,0,100,100);
scene.setBackgroundBrush(QColor(255,255,0,255));
scene.setItemIndexMethod(QGraphicsScene::NoIndex);
QRectF p(40,40,0.1,0.1);
QBrush brush(Qt::red);
QPen pen(Qt::red);

drawAxis();

QGraphicsRectItem *item;
for(long int i = 0;i<100000;i++)
{

item = scene.addRect(p,pen,brush);
item->setFlag(QGraphicsItem::ItemDoesntPropagateOpacityT oChildren);
}

view.setScene(&scene);
view.setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
view.setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
//view.setResizeAnchor(QGraphicsView::AnchorViewCent er);
view.setCacheMode(QGraphicsView::CacheBackground);
view.setViewportUpdateMode(QGraphicsView::Bounding RectViewportUpdate);
view.setOptimizationFlag(QGraphicsView::DontClipPa inter);
view.viewport()->setFocusProxy( this );
QVBoxLayout *l = new QVBoxLayout();
l->addWidget(&view);
l->setMargin(0);
l->setSpacing(0);
setLayout(l);

view.show();
}

void myWidget::resizeEvent(QResizeEvent *e)
{
//the view should show the whole scene at all times
QTransform t;
view.setTransform(t.scale(qreal(view.width()) / qreal(scene.sceneRect().width()), qreal(view.height()) / qreal(scene.sceneRect().height())));
QWidget::resizeEvent(e);

}



now this is way too slow. painting takes ages and when myWidget gets resized, it takes 10 secs until the view sets up again. The view has to show the whole scene at all times, so each time the view gets resizes, its transformation needs to be set so it shows the whole scene again.

I have tried to prevent the QGraphicsItem::paint to paint dots that dont need to be painted coz they are obscured:



void MyGraphicsItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget* w)
{

painter->setClipRect( option->exposedRect );
QGraphicsRectItem::paint( painter, option, w);
}


it still is too slow. I have experimented with almost all flags in QGraphicsView (viewportUpdateMode, CacheMode,....) but it didnt help.

Does anyone have a simple example (code snippet) with a scene that contains one million items and its view still paints and reacts to resizing events as fast as it is praised on the qt-website?

wysota
10th July 2009, 16:18
First of all I would set FullViewportUpdate instead of BoundingRectViewportUpdate. Second of all I would set a QGLWidget as the viewport of the view (if you do that, you can ignore the update mode, QGLWidget always updates the whole viewport). Don't do any clipping, don't cache the background if you're not drawing it. Use fitInView instead of what you currently have in resizeEvent. This should be enough to get you going but if not, you might want to consider using a plain widget instead of graphics view which currently doesn't give you much in exchange for some overhead it introduces.