Results 1 to 14 of 14

Thread: Performance problems with overlapping qgraphicsitems

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2008
    Posts
    29
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Performance problems with overlapping qgraphicsitems

    I was having the same problem. A fairly simple GraphicsView with ellipses and rects and so forth, where if I put more then about 10-20 items on the view it was very slow, and used 90% of the CPU. When I made the change above, things are working much better.

    If someone could please explain why I was seeing such slow behavior, and why this change helps, I would really appreciate it.

    Also it there something I could do to speed things up otherwise I would like to know about that as well.

    Is the difference between my project and 40,000 chips just the fact that in my project some of the GraphicsItems can overlap?

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

    Default Re: Performance problems with overlapping qgraphicsitems

    It's hard to say without seeing the actual code. My guess is that you should play a bit with different update modes of QGraphicsView or improve your bounding rect implementation.

  3. #3
    Join Date
    Apr 2008
    Posts
    29
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Performance problems with overlapping qgraphicsitems

    One question I had was why is this code helping, and after looking at the code again, it seems that all it is doing is replacing the paintEvent with a generic paint event that has default settings except for the boundingRect. This means that one of the properties of the paint event must have been responsible for the slow down. I will put a breakpoint into the method, and compare the events. This might give me a hint as to what is wrong.

    Here's the code I use to create my view:

    Qt Code:
    1. worksheetScene = new(QGraphicsScene);
    2. worksheetView = new(QWorksheetView);
    3.  
    4. // JSL - single sceneDrawCommon for common drawObject behavior
    5. sceneDrawCommon = new QDrawCommon();
    6. worksheetScene->addItem(sceneDrawCommon);
    7.  
    8. worksheetView->setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
    9. worksheetView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    10. worksheetView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    11. worksheetView->setScene(worksheetScene);
    12. worksheetView->setDragMode(QGraphicsView::RubberBandDrag);
    13. worksheetView->setAcceptDrops(true);
    14. worksheetView->show();
    15. setWidget(worksheetView);
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Apr 2008
    Posts
    29
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Performance problems with overlapping qgraphicsitems

    I seem to be getting multiple paintEvents infinitely whether there are any changes or not. I looked for an update call in my paint code, but didn't see one. Interestingly this only happens when I have at least one GraphicsItem in the view. When I have none, I don't get the paint loop. This explains both the slow behavior, and the CPU utilization, but I am not sure at all why I am getting the multiple paintEvents.

    Here's the code of my QDrawRect Object:

    Qt Code:
    1. QDrawRect::QDrawRect()
    2. {
    3. setFlag(QDrawRect::ItemIsMovable);
    4. setFlag(QDrawRect::ItemIsSelectable);
    5. drawData.boundsRect = boundingRect();
    6. drawData.shape = RECTSHAPE;
    7. }
    8.  
    9.  
    10. void QDrawRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    11. {
    12. QRectF joe = boundingRect();
    13.  
    14. painter->setRenderHint(QPainter::Antialiasing, itsWorksheet->wkAntiAliasing);
    15. setBrush(QBrush(drawData.color));
    16. QGraphicsRectItem::paint(painter, option, widget);
    17. paintHandles(this, painter);
    18. }
    19.  
    20. //
    21. // JSL - updatePosAndBounds updates the position and bounds based on the drawData
    22. //
    23. void QDrawRect::updatePosAndBounds()
    24. {
    25. qreal x, y, w, h;
    26.  
    27. x = drawData.boundsRect.left();
    28. y = drawData.boundsRect.top();
    29. w = drawData.boundsRect.right() - drawData.boundsRect.left();
    30. h = drawData.boundsRect.bottom() - drawData.boundsRect.top();
    31. setRect(x, y, w, h);
    32.  
    33. setPos(drawData.pos.x(), drawData.pos.y());
    34. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Apr 2008
    Posts
    29
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Performance problems with overlapping qgraphicsitems

    Figured out what it was. In the code above, the setBrush call was causing an additional update. I'm not sure quite why. If someone that knows wants to enlighten me, I would appreciate it, but when I don't do this during the paint call, I don't get the paintEvent loop. (Which fixes my CPU utilization issue as well.)

  6. #6
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Performance problems with overlapping qgraphicsitems

    You can use painter->setBrush() instead. There isn't much point in inheriting from QGraphicsRectItem. All it does is draw a rectangle, so you might as well do it yourself.

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

    Default Re: Performance problems with overlapping qgraphicsitems

    Changing any properties of the item while painting is a very bad idea. From what I see your item does the same the standard rect item does. Instead of discarding the QGraphicalRectItem inheritance as suggested, I'd suggest keeping the inheritance and using the class's methods to set its properties immediately when you know them (so in the constructor probably).

  8. #8
    Join Date
    Apr 2008
    Posts
    29
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Performance problems with overlapping qgraphicsitems

    Thanks for the replies. I think this makes sense to me now. I will avoid modifying properties on a GraphicsItem during the paint procedure. I'm still not sure why modifying the paintEvent as in the fix above seemed to speed things up, however.

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

    Default Re: Performance problems with overlapping qgraphicsitems

    Because a different region of the viewport has been scheduled for update. It's too complicated to go into details here. You may download Andreas Hanssen's presentation about QGV from last year's DevDays when different update modes are explained. There are some tips how to speed things up there as well.

  10. The following user says thank you to wysota for this useful post:

    stevel (5th May 2008)

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
  •  
Qt is a trademark of The Qt Company.