Results 1 to 20 of 80

Thread: GraphicsView performance problems

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

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

    What exactly do you mean by rubberbanding support?

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

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

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

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

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

    Gopala Krishna (21st February 2007)

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

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

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

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

  12. #11
    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 13:23.

  13. #12
    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 13:42. Reason: updated contents
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

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

Similar Threads

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