Page 1 of 4 123 ... LastLast
Results 1 to 20 of 72

Thread: QGraphicsScene/QGraphicsView performance problems

  1. #1
    Join Date
    Jan 2008
    Posts
    155
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QGraphicsScene/QGraphicsView performance problems

    Hi!

    I am trying to write a microchip CAD datafile display program using the QGraphics View framework. It is the first time i use this, and it seems to be very convenient, very little (of my own) code is needed to do what I want to do.
    However, my data files may range from small to quite large, anything between a few 100kb and up to a few 100Mb. At about 20Mb, my application begins to choke, i think mainly due to memory consumption. A Linux amd64 dual core system gets completely stuck when loading a 40Mb file, I had to terminate to avoid a complete system crash.
    My MacOSX ppc G5 dual core behaves just the same.
    I have not tried Windows yet.

    I use Qt4.3.3 and recently Qt4.4.0 snapshot, same results on each.

    I have a feeling there is a lot of stuff in the QGraphics View that is not needed for my app, I just need to draw, scroll and zoom, no select, move or transform.

    Is the QGraphics View framework the right thing to use for me?
    If so, can I turn off some of the bells and whistles to gain performance?
    Or should I use some different methods?

    Any suggestions are appreciated.
    MacOSX user dabbling with Linux and Windows.

  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/QGraphicsView performance problems

    If it chokes while loading the data, there is a chance the slowdown is not related to graphics view at all but instead to the loading routine. Use a profiler to find the bottleneck of the application. Also if you have multiple cores, try to design your application in such a way that all cores are used (especially when loading the data).

  3. #3
    Join Date
    Jan 2008
    Posts
    155
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsScene/QGraphicsView performance problems

    Just realized how not to use itemized data, so I am drawing in myQGraphicsView::drawBackground() instead. Now it doesn't use much memory, so I can load large files.
    Now the problem is back to performance speed again, and I need to check in my own code wether the shapes are in the visible view or not, and find a way to sift through the data real fast. Also, the scrolling is directly depending on how fast I can draw.
    I found that it was impossible to draw a mouse rubberband if GraphicsView was not cashed.
    Is there a way to cache the background drawing?
    MacOSX user dabbling with Linux and Windows.

  4. #4
    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/QGraphicsView performance problems

    Quote Originally Posted by bnilsson View Post
    Just realized how not to use itemized data, so I am drawing in myQGraphicsView::drawBackground() instead.
    But now you are not using the graphics view architecture at all. You could as well use a plain QWidget instead.

  5. #5
    Join Date
    Jan 2008
    Posts
    155
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsScene/QGraphicsView performance problems

    Yes, I realize that. Yet the way of using methods of zooming in/out and the scrollers are the same. which is convenient for me right now. I am only a Qt beginner, so I would need to study how to do the same with more "standard" methods. Will I gain much performance if I change to QWidget? I suspect I would never get it working for a 150Mb data file if I use QGraphicsItems with QGraphicsScene/QGraphicsView.

    BTW, I am using QDataStream to repeatedly read my data, even for updates. I guess this is not terribly efficient. Can you recommend another way?
    MacOSX user dabbling with Linux and Windows.

  6. #6
    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/QGraphicsView performance problems

    Quote Originally Posted by bnilsson View Post
    Will I gain much performance if I change to QWidget?
    You'll lose the overhead caused by everything that QGraphicsView has and QWidget has not.

    I suspect I would never get it working for a 150Mb data file if I use QGraphicsItems with QGraphicsScene/QGraphicsView.
    I'm not so sure. One graphics item takes about 50 bytes of memory on average. How many items would you have in a 150MB file? 1M? That's less than 100MB of RAM used for the items.

    BTW, I am using QDataStream to repeatedly read my data, even for updates. I guess this is not terribly efficient. Can you recommend another way?
    Hard to say without knowing what your data represents. But if you do that constantly then that is surely inefficient.

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

    Default Re: QGraphicsScene/QGraphicsView performance problems

    Quote Originally Posted by bnilsson View Post
    Is there a way to cache the background drawing?
    Qt Code:
    1. view.setCacheMode(QGraphicsView::CacheBackground);
    To copy to clipboard, switch view to plain text mode 

    But really you need to provide some more information. How many items, how complex are these items? Drawing them manually in drawbackground is not really the best way to go about it.

  8. #8
    Join Date
    Jan 2008
    Posts
    155
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsScene/QGraphicsView performance problems

    Typically, I might want to display a few thousand up to 10-20 million trapezoidal shapes, either as filled or empty, with different colours.

    When I have used itemized drawing, I do
    Qt Code:
    1. if(fill) scene->addPolygon(Trapezoid,AreaShotPen,AreaShotBrush);
    2. else scene->addPolygon(Trapezoid,AreaShotPen);
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. if(fill) scene->addRect(outRect,AreaShotPen, AreaShotBrush);
    2. else scene->addRect(outRect,AreaShotPen);
    To copy to clipboard, switch view to plain text mode 

    This should be simple, but about 1 million shapes seems to be the limit right now, both for memory consumption and the patience of the user.

    I am using QRectF and QPolygonF and true microns as internal numbers, would it improve performance if I use integer QRect and QPolygon instead?
    MacOSX user dabbling with Linux and Windows.

  9. #9
    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/QGraphicsView performance problems

    I wouldn't use polygons here. You have a pretty simple shape, you can create your own item class which will be much faster.

  10. #10
    Join Date
    Jan 2008
    Posts
    155
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default

    Quote:
    I suspect I would never get it working for a 150Mb data file if I use QGraphicsItems with QGraphicsScene/QGraphicsView.
    I'm not so sure. One graphics item takes about 50 bytes of memory on average. How many items would you have in a 150MB file? 1M? That's less than 100MB of RAM used for the items.
    I have a hard time believing that. A file of 600,000 shapes costs me about 500Mb of additional RAM, which indicates almost 1kb per item. I must be doing something seriously wrong....

    I wouldn't use polygons here. You have a pretty simple shape, you can create your own item class which will be much faster.
    Sorry, I am too new to Qt to implement such a thing. Could you direct me to some example?
    Also, polygons are mostly only about 10-20% of the total population, the rest are rectangles. Still worthwile to make a new class?
    Last edited by wysota; 5th January 2008 at 12:45. Reason: Merged two posts sent in a short period of time.
    MacOSX user dabbling with Linux and Windows.

  11. #11
    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/QGraphicsView performance problems

    Quote Originally Posted by bnilsson View Post
    I have a hard time believing that. A file of 600,000 shapes costs me about 500Mb of additional RAM, which indicates almost 1kb per item. I must be doing something seriously wrong....
    Because you are using QGraphicsPolygonItem. You can probably use something simpler instead.

    Quote Originally Posted by bnilsson View Post
    Sorry, I am too new to Qt to implement such a thing. Could you direct me to some example?
    See examples bundled with Qt.
    Also, polygons are mostly only about 10-20% of the total population, the rest are rectangles. Still worthwile to make a new class?
    Yes, because they are the most heavy of your items so they probably occupy about 50% of the memory used by items. A simple esitmate of the memory can be calculated as follows:
    total_per_item = sizeof(QGraphicsItem) + sizeof(data_stored_within_item) where the latter is:

    in case of a rect item: sizeof(QRectF) + sizeof(QPen) + sizeof(QBrush) = sizeof(QPointF)+sizeof(QSizeF) + sizeof(QPen)+sizeof(QBrush) = 2*sizeof(double) + 2*sizeof(double) + ...

    in case of a polygon item: sizeof(QPolygonF) + sizeof(QPen) + sizeof(QBrush) = sizeof(QVector<QPointF>) + ... = n * sizeof(QPointF) + ... = n*2*double + ... = at least twice as much as for QRectF with n>=4.

    So a rect item data occupies probably about 70-80 bytes and a polygon item probably around 100. Add all the additional stuff kept by all the classes to that and multiply by the number of items.

    BTW. Also consider using fewer more complex items instead of more simpler ones. Maybe you can "connect" your trapezoids into more complex shapes?
    Last edited by wysota; 5th January 2008 at 12:55.

  12. #12
    Join Date
    Jan 2008
    Posts
    155
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsScene/QGraphicsView performance problems

    Thanks for your engagement, but I don't seen go get anywhere.
    I managed to implement a subclass of QGraphicsItem, but it does not draw, I have no idea why. Could you help me?
    Qt Code:
    1. #ifndef SHAPEITEM_H
    2. #define SHAPEITEM_H
    3.  
    4. #include <QtGui>
    5. #include <QGraphicsItem>
    6. #include <QObject>
    7.  
    8. class ShapeItem : public QGraphicsItem
    9. {
    10.  
    11. public:
    12. ShapeItem(void);
    13. ShapeItem(const QRect *rect);
    14. ShapeItem(const QPolygon *poly);
    15.  
    16. QRectF boundingRect() const;
    17. QPainterPath shape(void) const;
    18.  
    19. virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    20. QWidget *widget);
    21. private:
    22.  
    23. int x0,y0,x1,y1,x2,y2,x3,y3;
    24. int xmin,ymin,xmax,ymax;
    25. };
    26.  
    27. #endif
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "shapeitem.h"
    2.  
    3. ShapeItem::ShapeItem(const QRect *rect)
    4. {
    5. x0 = rect->x();
    6. y0 = rect->y();
    7.  
    8. x1 = rect->x() + rect->width();
    9. y1 = y0;
    10.  
    11. x2 = x1;
    12. y2 = y0 + rect->height();
    13.  
    14. x3 = x0;
    15. y3 = y2;
    16.  
    17. xmin = x0;
    18. xmax = x1;
    19. ymin = y0;
    20. ymax = y2;
    21.  
    22. }
    23.  
    24. ShapeItem::ShapeItem(const QPolygon *poly)
    25. {
    26. poly->point(0,&x0,&y0);
    27. poly->point(1,&x1,&y1);
    28. poly->point(2,&x2,&y2);
    29. poly->point(3,&x3,&y3);
    30.  
    31. xmin = xmax = x0;
    32. ymin = ymax = y0;
    33.  
    34. if(x1<xmin) xmin = x1;
    35. if(x2<xmin) xmin = x2;
    36. if(x3<xmin) xmin = x3;
    37.  
    38. if(y1<ymin) ymin = y1;
    39. if(y2<ymin) ymin = y2;
    40. if(y3<ymin) ymin = y3;
    41.  
    42. if(x1>xmax) xmax = x1;
    43. if(x2>xmax) xmax = x2;
    44. if(x3>xmax) xmax = x3;
    45.  
    46. if(y1>ymax) ymax = y1;
    47. if(y2>ymax) ymax = y2;
    48. if(y3>ymax) ymax = y3;
    49.  
    50. }
    51.  
    52.  
    53. ShapeItem::boundingRect() const
    54. {
    55. qreal penWidth = 1;
    56. return QRectF(xmin - penWidth / 2, ymin - penWidth / 2,
    57. xmax + penWidth / 2, ymax + penWidth / 2);
    58. }
    59.  
    60. ShapeItem::shape() const
    61. {
    62. path.addRect(boundingRect());
    63. return path;
    64. }
    65.  
    66.  
    67. void
    68. ShapeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    69. QWidget *widget)
    70. {
    71. painter->drawLine ( x0,y0,x1,y1 );
    72. painter->drawLine ( x1,y1,x2,y2 );
    73. painter->drawLine ( x2,y2,x3,y3 );
    74. painter->drawLine ( x3,y3,x0,y0 );
    75. }
    To copy to clipboard, switch view to plain text mode 

    Not at all optimized, as soon as it draws me anything in the scene, I will test different variants. At this stage, numItems=0 in myQGraphicsItems::drawIntems, so I'm stuck.

    I am using it lile this:
    Qt Code:
    1. ShapeItem item(&outRect);
    2. scene->addItem(&item);
    3. //scene->addRect(outRect,solidBlue);
    To copy to clipboard, switch view to plain text mode 

    The commented-out line draws as it should, while the active lines does not.
    MacOSX user dabbling with Linux and Windows.

  13. #13
    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/QGraphicsView performance problems

    Create the item on heap.

    Qt Code:
    1. ShapeItem *item = new ShapeItem(...);
    2. scene->addItem(item);
    To copy to clipboard, switch view to plain text mode 

  14. #14
    Join Date
    Jan 2008
    Posts
    155
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsScene/QGraphicsView performance problems

    Thanks, now it is working.
    Unfortunately, now the app started gobbling up all memory even for a smaller file, so it got worse. I will have to figure out why...
    MacOSX user dabbling with Linux and Windows.

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsScene/QGraphicsView performance problems

    Quote Originally Posted by bnilsson View Post
    Unfortunately, now the app started gobbling up all memory even for a smaller file, so it got worse. I will have to figure out why...
    Use valgrind. The Massif tool might be especially helpful.

  16. #16
    Join Date
    Jan 2008
    Posts
    155
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsScene/QGraphicsView performance problems

    Use valgrind. The Massif tool might be especially helpful.
    For the moment I'm on MacOSX, so this is not applicable.
    MacOSX user dabbling with Linux and Windows.

  17. #17
    Join Date
    Jan 2008
    Posts
    155
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsScene/QGraphicsView performance problems

    The behaviour was a bit better using Qt4.4.0 than Qt4.3.3, so maybe it's not in my code entirely. But still in the 1Gb range for a 40Mb file.
    The behaviour was very strange indeed (excessive cpu and memory load when zooming IN) on my amd46 Debian using Qt4.3.3, and this particular problem was fixed by the Qt4.4.0 snapshot.

    Anyway, standard usage of QGraphicsView/QGraphiscScene was not dramatically improved using Qt4.4.0.
    Last edited by bnilsson; 5th January 2008 at 16:44.
    MacOSX user dabbling with Linux and Windows.

  18. #18
    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/QGraphicsView performance problems

    Could you provide a minimal compilable example reproducing the problem so that we might try it ourselves?

  19. #19
    Join Date
    Jan 2008
    Posts
    155
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsScene/QGraphicsView performance problems

    Sure, the project is not very big, I will try to prepare something.
    I am new to this forum, what are the provisions for uploading?
    MacOSX user dabbling with Linux and Windows.

  20. #20
    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/QGraphicsView performance problems

    Use the attachments feature ("Manage attachments" button below the advanced editor).

Similar Threads

  1. Performance problems with overlapping qgraphicsitems
    By brjames in forum Qt Programming
    Replies: 13
    Last Post: 4th May 2008, 21:42
  2. Poor performance with Qt 4.3 and Microsoft SQL Server
    By Korgen in forum Qt Programming
    Replies: 2
    Last Post: 23rd November 2007, 10:28
  3. GraphicsView performance problems
    By Gopala Krishna in forum Qt Programming
    Replies: 79
    Last Post: 8th August 2007, 17:32
  4. Replies: 2
    Last Post: 8th March 2007, 22:22
  5. Replies: 1
    Last Post: 4th October 2006, 16:05

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.