Results 1 to 14 of 14

Thread: Performance problems with overlapping qgraphicsitems

  1. #1
    Join Date
    Feb 2007
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Performance problems with overlapping qgraphicsitems

    I have an application where I've got 4 different types of QGraphicsItems. In the normal case, all 4 of these items have the exact same center.

    One is an unfilled circle with diameter 10.

    One is a cross that touches the midpoints of a 10x10 square

    One is an 'X' that touches the corners of a 10x10 square.

    The last is a filled in 3x3 square.

    I am experiencing much slowdown and %100 CPU utilization. I've looked at making sure boundingRect and shape return exact shape, but in the normal case, they do all collide. Is there anything I can do to increase performance?

    Or should this not be a problem and there is something else that might be causing my performance problem?

    Thanks!

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Performance problems with overlapping qgraphicsitems

    There must be something else thats causing the problem.
    You can add thousands of graphics items without any noticable slowdown.

    You might be leaking memory somewhere , or processing something infinitely.

  3. #3
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Performance problems with overlapping qgraphicsitems

    Quote Originally Posted by brjames View Post
    I am experiencing much slowdown and %100 CPU utilization. I've looked at making sure boundingRect and shape return exact shape, but in the normal case, they do all collide. Is there anything I can do to increase performance?

    Or should this not be a problem and there is something else that might be causing my performance problem?

    Thanks!
    As aamer pointed out there must be some other problem. Can you show some code ?
    Also have you tried the chip demo shipped by qt ? I ask this because it manages 40000 items(also proved to perform well for 2 million items) in 4 views which works well even on my low end comp. Do you experience slow down in demo ?
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  4. #4
    Join Date
    Feb 2007
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Performance problems with overlapping qgraphicsitems

    Hello, i eperimented performances problem with graphicsview and
    found a workaround wich can perhaps help,
    try it, it is very easy, you only have to reimplement paintEvent function like this
    in a subclass :

    Qt Code:
    1. class MyFasterGraphicView :public QGraphicsView
    2. {
    3. Q_OBJECT
    4. public:
    5. MyFasterGraphicView( ...);
    6. protected:
    7. ...
    8. void paintEvent ( QPaintEvent * event );
    9. ...
    10. };
    11.  
    12. void MyFasterGraphicView::paintEvent ( QPaintEvent * event )
    13. {
    14. QPaintEvent *newEvent=new QPaintEvent(event->region().boundingRect());
    15. QGraphicsView::paintEvent(newEvent);
    16. delete newEvent;
    17. }
    To copy to clipboard, switch view to plain text mode 

    I can explain the reason, is someone is interrested,

    Have fun,

    CyrBa
    Last edited by wysota; 24th February 2007 at 16:58. Reason: missing [code] tags

  5. #5
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Performance problems with overlapping qgraphicsitems

    Quote Originally Posted by cyrba View Post
    Hello, i eperimented performances problem with graphicsview and
    found a workaround wich can perhaps help,
    try it, it is very easy, you only have to reimplement paintEvent function like this
    in a subclass :

    Qt Code:
    1. class MyFasterGraphicView :public QGraphicsView
    2. {
    3. Q_OBJECT
    4. public:
    5. MyFasterGraphicView( ...);
    6. protected:
    7. ...
    8. void paintEvent ( QPaintEvent * event );
    9. ...
    10. };
    11.  
    12. void MyFasterGraphicView::paintEvent ( QPaintEvent * event )
    13. {
    14. QPaintEvent *newEvent=new QPaintEvent(event->region().boundingRect());
    15. QGraphicsView::paintEvent(newEvent);
    16. delete newEvent;
    17. }
    To copy to clipboard, switch view to plain text mode 

    I can explain the reason, is someone is interrested,

    Have fun,

    CyrBa
    Indeed the performance is better. Can somebody explain how this improves?
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  6. #6
    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?

  7. #7
    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: 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.

  8. #8
    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 

  9. #9
    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 

  10. #10
    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.)

  11. #11
    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.

  12. #12
    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: 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).

  13. #13
    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.

  14. #14
    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: 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.

  15. 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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.