Results 1 to 20 of 72

Thread: QGraphicsScene/QGraphicsView performance problems

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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.

  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

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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 

  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

    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.

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

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

  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

    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.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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?

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

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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).

  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

    What would be your primary platform?

    I will have to arrange some way for you to pick up the datafiles, they will be too large for an attachment. I think I have some space at my ISP's web hotel, I will do this tomorrow.
    What will be your primary project platform?

    Here is the project. It is currently tested only for Mac, I have not built this particular project for Linux yet.
    Attached Files Attached Files
    Last edited by wysota; 6th January 2008 at 00:03. Reason: Posts merged
    MacOSX user dabbling with Linux and Windows.

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.