Hi,
Actually this is continuation of this thread but the question is quite different.
I am running into performance problems when multiple items are moved. The peformance is better when I disable the children of resistor(nodes and text item). But I want those features in my app. Also the grid look corrupted when items are moved. Any suggestion for that ?
Here is the profile output
Flat profile:

Each sample counts as 0.01 seconds.
% cumulative self self total
time seconds seconds calls Ts/call Ts/call name
75.08 0.03 0.03 GridScene::drawBackground(QPainter*, QRectF const&)
25.03 0.04 0.01 Resistor::boundingRect() const
0.00 0.04 0.00 119168 0.00 0.00 QVarLengthArray<QLineF, 100>::realloc(int, int)
Here is the shorter version of the code. Select all items and move so that you can see what I mean. Please help me.

Qt Code:
  1. #include <QtGui>
  2.  
  3. class GridScene : public QGraphicsScene
  4. {
  5. public:
  6. GridScene(qreal x, qreal y, qreal w, qreal h)
  7. : QGraphicsScene(x, y, w, h)
  8. { }
  9.  
  10. protected:
  11. void drawBackground(QPainter *painter, const QRectF &rect)
  12. {
  13. const int gridSize = 25;
  14. painter->setPen(QPen(Qt::darkGreen,0));
  15. qreal left = int(rect.left()) - (int(rect.left()) % gridSize);
  16. qreal top = int(rect.top()) - (int(rect.top()) % gridSize);
  17.  
  18. QVarLengthArray<QLineF, 100> lines;
  19.  
  20. for (qreal x = left; x < rect.right(); x += gridSize)
  21. lines.append(QLineF(x, rect.top(), x, rect.bottom()));
  22. for (qreal y = top; y < rect.bottom(); y += gridSize)
  23. lines.append(QLineF(rect.left(), y, rect.right(), y));
  24.  
  25. painter->drawLines(lines.data(), lines.size());
  26. }
  27. };
  28.  
  29. class Node : public QGraphicsItem
  30. {
  31. public:
  32. Node(QPointF pos ,QGraphicsItem *par = 0, QGraphicsScene *sc = 0) : QGraphicsItem(par,sc)
  33. {
  34. setPos(pos);
  35. setFlags(0);
  36. setAcceptedMouseButtons(0);
  37. }
  38.  
  39. void paint(QPainter* p,const QStyleOptionGraphicsItem *, QWidget *)
  40. {
  41. p->setPen(Qt::darkRed);
  42. p->drawEllipse(QRectF(-3, -3, 2*3, 2*3));
  43. }
  44.  
  45. QPainterPath shape() const
  46. {
  47. path.addEllipse(QRectF(-3, -3, 2*3, 2*3));
  48. return path;
  49. }
  50.  
  51. QRectF boundingRect() const
  52. {
  53. return QRectF(-3, -3, 2*3, 2*3);
  54. }
  55.  
  56. };
  57.  
  58. class Resistor : public QGraphicsItem
  59. {
  60. public:
  61. Resistor(QGraphicsItem *par = 0, QGraphicsScene *scene = 0) : QGraphicsItem(par,scene)
  62. {
  63. setFlags(ItemIsMovable | ItemIsSelectable | ItemIsFocusable);
  64. // comment the following 4 lines to see the performance difference
  65. QGraphicsTextItem *t = new QGraphicsTextItem("R1 = 100k",this,scene);
  66. t->setPos(0,-35);
  67. new Node(QPointF(-30,0),this,scene);
  68. new Node(QPointF(30,0),this,scene);
  69. }
  70.  
  71. void paint(QPainter *p, const QStyleOptionGraphicsItem *o, QWidget *)
  72. {
  73. if(!(o->state & QStyle::State_Open))
  74. p->setPen(Qt::darkBlue);
  75. if(o->state & QStyle::State_Selected)
  76. p->setPen(QPen(Qt::darkGray,1));
  77.  
  78. p->drawLine(-18, -9, 18, -9);
  79. p->drawLine( 18, -9, 18, 9);
  80. p->drawLine( 18, 9,-18, 9);
  81. p->drawLine(-18, 9,-18, -9);
  82. p->drawLine(-27, 0,-18, 0);
  83. p->drawLine( 18, 0, 27, 0);
  84. }
  85.  
  86. QRectF boundingRect() const
  87. {
  88. qreal pw = 0.5;
  89. return QRectF(-27,-9,54,18).adjusted(-pw,-pw,pw,pw);
  90. }
  91. };
  92.  
  93. int main(int argc,char *argv[])
  94. {
  95. QApplication app(argc,argv);
  96. GridScene scene(0,0,1024,768);
  97. for(int j = 2; j < 4; ++j)
  98. for(int i = 1; i <11; ++i)
  99. {
  100. Resistor *r = new Resistor(0,&scene);
  101. r->setPos(j*100, i * 50);
  102. }
  103. QGraphicsView *view = new QGraphicsView(&scene);
  104. view->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
  105. view->setDragMode(QGraphicsView::RubberBandDrag);
  106. view->show();
  107. return app.exec();
  108. }
To copy to clipboard, switch view to plain text mode