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

    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

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

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

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

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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
  •  
Qt is a trademark of The Qt Company.