Page 4 of 4 FirstFirst ... 234
Results 61 to 80 of 80

Thread: GraphicsView performance problems

  1. #61
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: GraphicsView performance problems

    Depends on where your bottleneck is. (And that means profiling)

    If it is drawing, as small a boundingrect as possible.
    If it is the calculation of what has to be redrawn, bigger roundingrect is better.

    But as a general rule: as small boundingrect as possible. This way you do not get in the way when optimization deeper in the framework make all your little workarounds obsolete ;-)


    As 4.3, it "felt" faster to me (in valgrind). But I did not compare 1:1, and it might be that I even compared an optimized build to an debug built. Best test yourself ;-)

  2. #62
    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: GraphicsView performance problems

    How does then setting the bounding rect to include children impreoves performance ??
    I got a noticable improvement by including the childrens rect within bounding rect

  3. #63
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: GraphicsView performance problems

    Quote Originally Posted by aamer4yu View Post
    How does then setting the bounding rect to include children impreoves performance ??
    I got a noticable improvement by including the childrens rect within bounding rect

    Read this especially the "EDIT:" part.

    If you move, graphicsview will repaint the dirty areas, (i.e. the areas occupied by the moving/changing items).

    Short Version:
    Say you have 30 items, that means Graphicsview would need to repaint 60 (at most) regions on screen (where the objects came from, where the objects are now). Graphicsview does as far as I know optimize that, i.e. it will check if you can combine regions, as to not repaint the same thing multiple times.
    • If you have now 30 completely overlapping items, it will be combined to a single big one -> only one repaint.
    • If you have always 3 that are overlapping, for example an item and its child items -> only 10 repaints.

    Less repaints => higher performance, especially if the repaints are not optimized.



    But: Do not make boundingrects bigger to include child items! Future optimizations in QGraphicsView will probably make that actually slower, and even now you may actually get more repaints.
    Rather: Think if you really need childitems, or it would make sense to combine them into on item. AND: test if it actually performs better after the change!

    USE KCACHEGRIND to find out where your bottlenecks really are.
    Last edited by camel; 20th February 2007 at 17:45.

  4. #64
    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: GraphicsView performance problems

    Probably I am asking too much from trolls or it is heights of optimization!!
    How about rubberbanding support while moving items in future version of qt ? This will surely reduce number of repaints. Anyway I am planning to do this to my app after the port reach some descent stage.

    I say this because, the app I am porting(obviously from qt3.3 - 4.2) - qucs uses this technique in qt3 version and the performance is very good. And actually it uses lots of repaints() (instead of update()) and also the code doesn't optimize update by using rect version of update - I mean QWidget::update(QRect r). And best of all, it doesn't use QCanvas. Yet it works fine even on low end pc's.
    That is the reason, I am asking question about performance so that the ported version does better than previous one.
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  5. #65
    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: GraphicsView performance problems

    What exactly do you mean by rubberbanding support?

  6. #66
    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: GraphicsView performance problems

    Quote Originally Posted by wysota View Post
    What exactly do you mean by rubberbanding support?
    What I mean is, the elements which are moving can be drawn with something like QPainter::rasterOP() set to NotROP of qt3 and QPen being DotLine. While updating position of item, first the item can be drawn with above flags again at previous position and then in new position.

    BTW, did I use a wrong word for this ? (I mean rubberbanding)
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  7. #67
    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: GraphicsView performance problems

    Quote Originally Posted by Gopala Krishna View Post
    What I mean is, the elements which are moving can be drawn with something like QPainter::rasterOP() set to NotROP of qt3 and QPen being DotLine. While updating position of item, first the item can be drawn with above flags again at previous position and then in new position.
    I think the problem is not in painting but in reconstructing the index, so it wouldn't help much. The raster operation (XOR here) does make sense only if objects don't overlap. When they do, you'll get trash when moving them away. It's better to redraw the region.

    BTW, did I use a wrong word for this ? (I mean rubberbanding)
    Yes, sort of Rubber band is used when talking about selecting items (because of a shape that expands if you drag it allowing to select an area - see QRubberBand).

  8. #68
    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: GraphicsView performance problems

    Quote Originally Posted by wysota View Post
    I think the problem is not in painting but in reconstructing the index, so it wouldn't help much. The raster operation (XOR here) does make sense only if objects don't overlap. When they do, you'll get trash when moving them away. It's better to redraw the region.
    Oh ok. Thanks. I didn't think about this.
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  9. #69
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: GraphicsView performance problems

    Just want to share a little thing I just read about on qt4-preview-feedback, for all those who like to play around with snapshots. (And to get it into the archive here too, if anyone looks this thread up later ;-)

    In this message Andreas mentions a nice flag you can set on the GraphicsView QGraphicsView::SmartViewportUpdate

    Andreas explains the behaviour (a small addition of mine is in cursive)
    at a certain threshold (of number of dirty rects)(currently set to 50 rects), the bounding rect is used instead.
    You can use it like this to try it out:
    Qt Code:
    1. #if QT_VERSION >= 0x040300
    2. view->setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
    3. #endif
    To copy to clipboard, switch view to plain text mode 



    Another thing you might want to try, is the QGraphicsView::OptimizationFlags. (especially if you do all your clipping yourself, and set the painter state each time anyway, such as I did in my version of the example)
    But remember: setting these flags allow you to shoot your own leg off much easier, if you are not carefull...
    Qt Code:
    1. #if QT_VERSION >= 0x040300
    2. view->setOptimizationFlags(QGraphicsView::DontClipPainter | QGraphicsView::DontSavePainterState);
    3. #endif
    To copy to clipboard, switch view to plain text mode 


    From my non-scientific try-outs, both make it feel faster to me, individually as well as combined.

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

    Gopala Krishna (21st February 2007)

  11. #70
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: GraphicsView performance problems

    I thought about starting a page in the wiki about how to get the most from your use of QGraphicsView (so that all the little nice tidbits can be collected somewhere)...

    What would be the best name for it?
    "Optimizing QGraphicsView", is kind of wrong

    "Optimizing your use of QGraphicsView" is correct, but kind-of long...
    "Good coding practices for QGraphicsView"...ditto...


    Probably the best thing would be to also have a
    "Optimizing your use of QPainter" section to be able to refer to it...

  12. #71
    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: GraphicsView performance problems

    How about "Optimizing techniques with GraphicsView"
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  13. #72
    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: GraphicsView performance problems

    Quote Originally Posted by camel View Post
    Just want to share a little thing I just read about on qt4-preview-feedback, for all those who like to play around with snapshots. (And to get it into the archive here too, if anyone looks this thread up later ;-)

    In this message Andreas mentions a nice flag you can set on the GraphicsView QGraphicsView::SmartViewportUpdate
    Hey thanks for the nice link. One of the thing qt is good at is to make us eagerly wait for the next version !!!!!
    And hence it pulls you towards using the snapshots !
    I am surely gonna try this weekend !
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  14. #73
    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: GraphicsView performance problems

    Today I found some time to play again with all codes in this thread. Just then I thought of one more technique to draw the grid which I think should increase the performance. But I couldn't see major difference while trying it out.
    The main idea is to create big pixmap and draw the grid to it in the constructor. In drawBackground() we can just draw relevant part of pixmap. I feel this way, even if the grid size is reduced, the performance remains same. Only thing is I compensate ram for speed which is ok for me. To combat this, probably we can reduce pixmap size to medium one and take necessary actions when the backgroundRect to be drawn is larger.
    Anyway the code is here.

    Note: This is modified version of camel's code
    Qt Code:
    1. #include <QtGui>
    2. #include <cmath>
    3.  
    4. namespace {
    5. static inline int gridFloor(const qreal& value)
    6. {
    7. if (value < 0) {
    8. return -1 * static_cast<int>(std::ceil(qAbs(value)));
    9. } else {
    10. return static_cast<int>(std::floor(value));
    11. }
    12. }
    13.  
    14. static inline int gridCeil(const qreal &value)
    15. {
    16. if (value < 0) {
    17. return -1 * static_cast<int>(std::floor(qAbs(value)));
    18. } else {
    19. return static_cast<int>(std::ceil(value));
    20. }
    21. }
    22. }
    23.  
    24. class GridScene : public QGraphicsScene
    25. {
    26. public:
    27. GridScene(qreal x, qreal y, qreal w, qreal h)
    28. : QGraphicsScene(x, y, w, h)
    29. {
    30. setItemIndexMethod(QGraphicsScene::NoIndex);
    31. setupCache();
    32. }
    33.  
    34. protected:
    35. void setupCache()
    36. {
    37. const int gridSize = 25;
    38. backgroundCache = QPixmap(1024,768);
    39. backgroundCache.fill(Qt::white);
    40.  
    41. QPainter painter(&backgroundCache);
    42. painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    43. painter.setPen(QPen(Qt::darkGreen,0));
    44. painter.setBrush(Qt::NoBrush);
    45.  
    46. for (int x = 0; x < 1024; x += gridSize)
    47. painter.drawLine(x,0,x,768);
    48. for (int y = 0; y < 768; y += gridSize)
    49. painter.drawLine(0,y,1024,y);
    50.  
    51. }
    52. void drawBackground(QPainter *painter, const QRectF &rect)
    53. {
    54. const int realLeft = gridFloor(rect.left());
    55. const int realRight = gridCeil(rect.right());
    56. const int realTop = gridFloor(rect.top());
    57. const int realBottom = gridCeil(rect.bottom());
    58. QRect realRect(realLeft,realTop,realRight-realLeft,realBottom-realTop);
    59. painter->drawPixmap(realRect,backgroundCache,realRect);
    60. }
    61.  
    62. private:
    63. QPixmap backgroundCache;
    64. };
    65.  
    66. namespace {
    67. static inline QPainterPath constructNodeShape(const QRectF& elipseRect)
    68. {
    69. path.addEllipse(elipseRect);
    70. return path;
    71. }
    72. }
    73.  
    74. class Node : public QGraphicsItem
    75. {
    76. public:
    77. Node(QGraphicsItem *par = 0, QGraphicsScene *sc = 0)
    78. : QGraphicsItem(par,sc),
    79. elipseRect(-4.0, -4.0, 8.0, 8.0),
    80. elipsePath(constructNodeShape(QRectF(-3.0, -3.0, 2.0*3.0, 2.0*3.0))),
    81. elipseShape(constructNodeShape(elipseRect)),
    82. nodePen(Qt::darkRed),
    83. nodeBrush(Qt::NoBrush)
    84. {
    85. setFlags(0);
    86. setAcceptedMouseButtons(0);
    87. }
    88.  
    89. void paint(QPainter* p,const QStyleOptionGraphicsItem *o, QWidget *)
    90. {
    91. p->setPen(nodePen);
    92. p->setBrush(nodeBrush);
    93. p->setOpacity(1.0);
    94.  
    95. p->setClipRect(o->exposedRect);
    96. p->drawPath(elipsePath);
    97. }
    98.  
    99. QPainterPath shape() const
    100. {
    101. return elipseShape;
    102. }
    103.  
    104. QRectF boundingRect() const
    105. {
    106. return elipseRect;
    107. }
    108.  
    109. protected:
    110. const QRectF elipseRect;
    111. const QPainterPath elipsePath;
    112. const QPainterPath elipseShape;
    113. const QPen nodePen;
    114. const QBrush nodeBrush;
    115. };
    116.  
    117.  
    118. namespace {
    119. static inline QPainterPath constructResistorPath()
    120. {
    121. QPainterPath resistorPath;
    122. resistorPath.addRect(QRectF(-18.0, -9.0, 36.0, 18.0));
    123. resistorPath.moveTo(-27, 0);
    124. resistorPath.lineTo(-18, 0);
    125. resistorPath.moveTo(18, 0);
    126. resistorPath.lineTo(27, 0);
    127. return resistorPath;
    128. }
    129. }
    130.  
    131. class Resistor : public QGraphicsItem
    132. {
    133. public:
    134. Resistor(QGraphicsItem *par = 0, QGraphicsScene *scene = 0)
    135. : QGraphicsItem(par,scene),
    136. resistorPath(constructResistorPath()),
    137. boundingBox(resistorPath.boundingRect().adjusted(-1, -1, 1, 1)),
    138. resistorSelectedPen(Qt::darkBlue),
    139. resistorOpenPen(Qt::darkGray,1),
    140. resistorBrush(Qt::NoBrush)
    141. {
    142. setFlags(ItemIsMovable | ItemIsSelectable | ItemIsFocusable);
    143. // comment the following 4 lines to see the performance difference
    144. QGraphicsTextItem *t = new QGraphicsTextItem("R1 = 100k",this,scene);
    145. t->setPos(0,-35);
    146. Node * node = new Node(this,scene);
    147. node->setPos(QPointF(-30,0));
    148. node = new Node(this,scene);
    149. node->setPos(QPointF(30,0));
    150. }
    151.  
    152. void paint(QPainter *p, const QStyleOptionGraphicsItem *o, QWidget *)
    153. {
    154. if(!(o->state & QStyle::State_Open))
    155. p->setPen(resistorOpenPen);
    156. if(o->state & QStyle::State_Selected)
    157. p->setPen(resistorSelectedPen);
    158.  
    159. p->setClipRect( o->exposedRect );
    160. p->setBrush(resistorBrush);
    161. p->drawPath(resistorPath);
    162. }
    163.  
    164. QRectF boundingRect() const
    165. {
    166. return boundingBox;
    167. }
    168.  
    169. private:
    170. const QPainterPath resistorPath;
    171. const QRectF boundingBox;
    172. const QPen resistorSelectedPen;
    173. const QPen resistorOpenPen;
    174. const QBrush resistorBrush;
    175. };
    176.  
    177. int main(int argc,char *argv[])
    178. {
    179. QApplication app(argc,argv);
    180. GridScene scene(0,0,1024,768);
    181. for(int j = 2; j < 4; ++j)
    182. for(int i = 1; i <11; ++i)
    183. {
    184. Resistor *r = new Resistor(0,&scene);
    185. r->setPos(j*100, i * 50);
    186. }
    187. QGraphicsView *view = new QGraphicsView(&scene);
    188. view->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    189. view->setDragMode(QGraphicsView::RubberBandDrag);
    190. view->show();
    191. return app.exec();
    192. }
    To copy to clipboard, switch view to plain text mode 
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  15. #74
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: GraphicsView performance problems

    What happens if you make the window larger than the pixmap? :-)

    Thing is, I would imagine that most draws are actually rather small pieces. (Fixing for moving items), so I do not know if it is faster to always work with the one big pixmap.

    Probably best would be something like 3 or 4 gridsizes as a cache...but I have not tested it out...
    Last edited by camel; 22nd February 2007 at 14:23.

  16. #75
    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: GraphicsView performance problems

    While experimenting further I noticed one of the major neglected thing.
    Just add this
    Qt Code:
    1. view->scale(4.0,4.0);
    To copy to clipboard, switch view to plain text mode 
    And now it is very very slow!!! Why ? Can someone explain this ?
    Also the the gridlines are quite thicker which I don't want .
    Please some one help me

    EDIT: This happens only in pixmap version of grids.
    Last edited by Gopala Krishna; 22nd February 2007 at 14:42. Reason: updated contents
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  17. #76
    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: GraphicsView performance problems

    Quote Originally Posted by Gopala Krishna View Post
    Just add this
    Qt Code:
    1. view->scale(4.0,4.0);
    To copy to clipboard, switch view to plain text mode 
    And now it is very very slow!!! Why ? Can someone explain this ?
    Sure, you introduced matrix transformations which makes everything more computation intensive (the pixmap is scaled).

    Also the the gridlines are quite thicker which I don't want .
    Please some one help me
    There is nothing you can do when using a pixmap to change it. The pixmap just gets scaled and all pixels get four times bigger in each direction.

  18. #77
    Join Date
    Jul 2007
    Posts
    39
    Thanks
    10

    Default Re: GraphicsView performance problems

    Hi

    I posted in the old thread too.

    Did you find a solution to draw the grid efficiently? I am running into the same problem. Also, when I zoomIn or zoomOut, my grid spacing is irregular.

    Kindly help me with suggestions.

    Thanks
    Arjun

  19. #78
    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: GraphicsView performance problems

    I solved my problem by a change in representation of grids. Now i use points with no brush and pen of 0 width to represent grids instead of lines. This has given me significant improvement. Also i switched from qt4.2 to qt4.3 , which gave me freedom to optimize the view more. Setting the view's update mode to smart mode remarkably improved the performance and i no longer need to optimize it anymore

    As far as your problem is concerned can you give me some details of your application ? Some code or screenshots definitely help us understand your problem
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  20. The following user says thank you to Gopala Krishna for this useful post:

    arjunasd (8th August 2007)

  21. #79
    Join Date
    Jul 2007
    Posts
    39
    Thanks
    10

    Default Re: GraphicsView performance problems

    Hi

    I did the changes you suggested and the grid works fine now.

    I have another problem though. I have basic shapes like circle, ellipse, rectangle etc in my view and they are drawn with a penwidth of 2. When I zoomin, the rectangle is fine but the curves of circle and ellipse appear discontinuous.

    I know zooming will decrease the quality. But is there any way to overcome this and make the circle continuous.

    Thanks
    Arjun

  22. #80
    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: GraphicsView performance problems

    Have you enabled antialiasing ?
    Qt Code:
    1. view->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    To copy to clipboard, switch view to plain text mode 
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  23. The following user says thank you to Gopala Krishna for this useful post:

    arjunasd (8th August 2007)

Similar Threads

  1. Performance problems with overlapping qgraphicsitems
    By brjames in forum Qt Programming
    Replies: 13
    Last Post: 4th May 2008, 22:42
  2. QT GraphicsView Help
    By mistertoony in forum Qt Programming
    Replies: 15
    Last Post: 15th February 2007, 05:17
  3. Replies: 1
    Last Post: 4th October 2006, 17:05
  4. QT4 Plugins - problems, problems
    By NormanDunbar in forum Qt Programming
    Replies: 6
    Last Post: 9th May 2006, 16:39
  5. Increasing performance from Qtextedit, listview, etc?
    By taylor34 in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2006, 11:20

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.