First do this (or something simmilar):
Qt Code:
  1. void drawLines(QPainter *painter, QVarLengthArray<QLineF, 100> &lines){
  2. painter->drawLines(lines.data(), lines.size());
  3. }
  4.  
  5. void setupArray(QVarLengthArray<QLineF, 100> &lines){
  6. const int gridSize = 25;
  7. qreal left = int(rect.left()) - (int(rect.left()) % gridSize);
  8. qreal top = int(rect.top()) - (int(rect.top()) % gridSize);
  9. for (qreal x = left; x < rect.right(); x += gridSize)
  10. lines.append(QLineF(x, rect.top(), x, rect.bottom()));
  11. for (qreal y = top; y < rect.bottom(); y += gridSize)
  12. lines.append(QLineF(rect.left(), y, rect.right(), y));
  13. }
  14. void drawBackground(QPainter *painter, const QRectF &rect){
  15. painter->setPen(QPen(Qt::darkGreen,0));
  16. QVarLengthArray<QLineF, 100> lines;
  17. setupArray(lines);
  18. drawLines(painter, lines);
  19. }
To copy to clipboard, switch view to plain text mode 

This should allow you to measure how much time does it take to setup the array and how much to paint it.