1

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.

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.

This is how I do it at the moment, I hate this but it's the only way I can think of doing it...

void Plotter::drawGrid() {
    unsigned int i;

    QGraphicsLineItem *line;
    QGraphicsTextItem *text;

    char num[11];
    QString label;

    unsigned int width = scene->sceneRect().width();
    unsigned int height = scene->sceneRect().height();

    removeGrid();

    for (i = 150; i < width; i+= 10) {
        line = new QGraphicsLineItem(i, 0, i, scene->sceneRect().height(), 0, scene);
        line->setPen(QPen(QColor(0xdd,0xdd,0xdd)));
        line->setZValue(0);

        _itoa_s(i - 150, num, 10);
        label = num;
        label += " ms";

        text = new QGraphicsTextItem(label, 0, scene);
        text->setDefaultTextColor(Qt::white);
        text->setX(i);
        text->setY(height - 10);
        text->setZValue(2);
        text->setScale(0.2);

        //pointers to items stored in list for removal later.
        gridList.append(line);
        gridList.append(text);
    }
    for (i = 0; i < height; i+= 10) {
        line = new QGraphicsLineItem(150, i, width, i, 0, scene);
        line->setPen(QPen(QColor(0xdd,0xdd,0xdd)));
        line->setZValue(0);
        gridList.append(line);
    }
}

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.

Luke
  • 2,434
  • 9
  • 39
  • 64

1 Answers1

1

I ended up using a 10x10 square pixmap and drew it as the backgroundBrush on my QGraphicsView, as is suggested in the link in the comment under my initial question.

Luke
  • 2,434
  • 9
  • 39
  • 64