2012-09-24 33 views
1

我想在Qt中的QGraphicsScene中繪製一個10毫秒的網格。我對Qt不是很熟悉......這是我第一次使用它,只是因爲應用程序需要在Windows和Linux之間移植。Qt中的高效網格

我實際上沒有繪製網格的問題,它只是網格變大時的性能。如果/當新數據加載到要顯示的程序中時,網格必須能夠更改大小以適應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()寬()獲取然而,應用程序變得非常緩慢。我曾嘗試過使用QGLWidget,但速度的提升最多隻能處於邊緣。

+1

之前已經提過類似的問題:http://stackoverflow.com/questions/12477082/render-qgraphicsscene-according-to-zoom-level – cmannett85

+1

提示:用_itoa_s替代部分,比如'label + = QString (「%1 ms」)。arg(i - 150);' –

回答

1

我最終使用了一個10x10的方形像素圖,並將其作爲backgroundBrush繪製在我的QGraphicsView上,如我在最初的問題下的評論鏈接中所建議的那樣。