1

我试图在 Qt 的 QGraphicsScene 中绘制一个 10 毫秒的网格。我对 Qt 不是很熟悉……这是我第一次使用它,只是因为应用程序需要在 Windows 和 Linux 之间移植。

我实际上没有绘制网格的问题,这只是网格变大时的性能。如果/当新数据加载到要显示的程序中时,网格必须能够更改大小以适应 SceneRect。

我现在就是这样做的,我讨厌这样做,但这是我能想到的唯一方法......

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);
    }
}

然而,当 scene->sceneRect().width() 变得太大时,应用程序变得非常缓慢。我曾尝试使用 QGLWidget,但速度上的改进充其量是微不足道的。

4

1 回答 1

1

我最终使用了一个 10x10 方形像素图并将其绘制为我的 QGraphicsView 上的 backgroundBrush,正如我最初问题下评论中的链接中所建议的那样。

于 2012-09-25T11:37:07.067 回答