Results 1 to 2 of 2

Thread: QGraphicsScene too slow

  1. #1
    Join Date
    Jul 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default QGraphicsScene too slow

    Hello there

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

    Qt Code:
    1. void myWidget::setupView()
    2. {
    3. scene.setSceneRect(0,0,100,100);
    4. scene.setBackgroundBrush(QColor(255,255,0,255));
    5. scene.setItemIndexMethod(QGraphicsScene::NoIndex);
    6. QRectF p(40,40,0.1,0.1);
    7. QBrush brush(Qt::red);
    8. QPen pen(Qt::red);
    9.  
    10. drawAxis();
    11.  
    12. for(long int i = 0;i<100000;i++)
    13. {
    14.  
    15. item = scene.addRect(p,pen,brush);
    16. item->setFlag(QGraphicsItem::ItemDoesntPropagateOpacityToChildren);
    17. }
    18.  
    19. view.setScene(&scene);
    20. view.setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
    21. view.setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
    22. //view.setResizeAnchor(QGraphicsView::AnchorViewCenter);
    23. view.setCacheMode(QGraphicsView::CacheBackground);
    24. view.setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
    25. view.setOptimizationFlag(QGraphicsView::DontClipPainter);
    26. view.viewport()->setFocusProxy( this );
    27. l->addWidget(&view);
    28. l->setMargin(0);
    29. l->setSpacing(0);
    30. setLayout(l);
    31.  
    32. view.show();
    33. }
    34.  
    35. void myWidget::resizeEvent(QResizeEvent *e)
    36. {
    37. //the view should show the whole scene at all times
    38. QTransform t;
    39. view.setTransform(t.scale(qreal(view.width()) / qreal(scene.sceneRect().width()), qreal(view.height()) / qreal(scene.sceneRect().height())));
    40. QWidget::resizeEvent(e);
    41.  
    42. }
    To copy to clipboard, switch view to plain text mode 

    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:aint to paint dots that dont need to be painted coz they are obscured:

    Qt Code:
    1. void MyGraphicsItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget* w)
    2. {
    3.  
    4. painter->setClipRect( option->exposedRect );
    5. QGraphicsRectItem::paint( painter, option, w);
    6. }
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsScene too slow

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 12
    Last Post: 7th September 2011, 16:37
  2. Replies: 0
    Last Post: 5th March 2009, 06:54
  3. in QGraphicsScene matrix of other QGraphicsScene
    By Noxxik in forum Qt Programming
    Replies: 5
    Last Post: 15th February 2009, 17:27
  4. Why is drawText so terribly slow?
    By Ntoskrnl in forum Qt Programming
    Replies: 8
    Last Post: 1st August 2008, 19:15
  5. QGraphicsScene / QGraphicsView speed after resize
    By themolecule in forum Qt Programming
    Replies: 1
    Last Post: 21st July 2007, 23:46

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.