title: Efficient Grid in Qt
body: <p>I am trying to draw a 10 millisecond grid in a QGraphicsScene in Qt. I am not very familiar with Qt... it's the first time I used it, and only because the application needs to be portable between Windows and Linux.</p>
  
  <p>I actually don't have a problem drawing the grid, it's just the performance when the grid gets big. The grid has to be able to change size to fit the SceneRect if/when new data is loaded into the program to be displayed.</p>
  
  <p>This is how I do it at the moment, I hate this but it's the only way I can think of doing it... </p>
  
  <pre><code>void Plotter::drawGrid() {
      unsigned int i;
  
      QGraphicsLineItem *line;
      QGraphicsTextItem *text;
  
      char num[11];
      QString label;
  
      unsigned int width = scene-&gt;sceneRect().width();
      unsigned int height = scene-&gt;sceneRect().height();
  
      removeGrid();
  
      for (i = 150; i &lt; width; i+= 10) {
          line = new QGraphicsLineItem(i, 0, i, scene-&gt;sceneRect().height(), 0, scene);
          line-&gt;setPen(QPen(QColor(0xdd,0xdd,0xdd)));
          line-&gt;setZValue(0);
  
          _itoa_s(i - 150, num, 10);
          label = num;
          label += " ms";
  
          text = new QGraphicsTextItem(label, 0, scene);
          text-&gt;setDefaultTextColor(Qt::white);
          text-&gt;setX(i);
          text-&gt;setY(height - 10);
          text-&gt;setZValue(2);
          text-&gt;setScale(0.2);
  
          //pointers to items stored in list for removal later.
          gridList.append(line);
          gridList.append(text);
      }
      for (i = 0; i &lt; height; i+= 10) {
          line = new QGraphicsLineItem(150, i, width, i, 0, scene);
          line-&gt;setPen(QPen(QColor(0xdd,0xdd,0xdd)));
          line-&gt;setZValue(0);
          gridList.append(line);
      }
  }
  </code></pre>
  
  <p>When scene->sceneRect().width() gets too big, however, the application becomes very sluggish. I have tried using a QGLWidget, but the improvements in speed are marginal at best.</p>