2012-11-17 107 views
1

我想繪製彩色圖塊作爲QGraphicsscene的背景,並使用QGraphicsView爲場景提供平移和縮放功能。首先我使用QGraphicItems繪製每個圖塊。因爲我有很多瓷磚平移或縮放但因爲我並不需要修改瓦時的任何部分,這是一個相當性能問題之後,我切換到使用下面的代碼生成的QPixmap:在「等效」QPixmap上繪製QGraphicItems的最快方法

void plotGrid(){ 
    Plotable::GraphicItems items; 
    append(items,mParticleFilter.createGridGraphics()); 
    append(items,mParticleFilter.getRoi().mRectangle.createGraphics(greenPen())); 
    scaleItems(items,1.0,-1.0); 
    QGraphicsScene scene; 
    showItemsOnScene(items,&scene); 
    QRectF boundingRect = scene.itemsBoundingRect(); 
    double cScale = ceil(1920.0/boundingRect.size().width()); 
    QSize size(boundingRect.size().toSize()*cScale); 
    QPixmap pixmap(size); 
    pixmap.fill(Qt::transparent); 
    QPainter p(&pixmap); 
    //p.setRenderHint(QPainter::Antialiasing); 
    scene.render(&p); 
    p.end(); 
    QGraphicsPixmapItem* item = new QGraphicsPixmapItem(pixmap); 
    item->setOffset(boundingRect.topLeft()*cScale); 
    item->scale(1/cScale,1/cScale); 
    mpView->showOnScene(item); 
    } 

雖然這解決了縮放和平移問題,生成像素映射的時間引入了一些重要的延遲,可能是因爲我先創建一個場景然後再渲染它。從QGraphicItems開始快速生成QPixmap有沒有更快的方法?

只是爲了保持完整性的瓦片的圖像:enter image description here

回答

0

所以,我終於得到了至少過去使用中間的場景。以下代碼僅依賴於QPainter來渲染像素圖。我的主要問題是要正確地完成所有的轉換。否則它是非常直接的...... 這個版本在我的方案中將處理時間減半到500ms。 450毫秒花在繪畫物品上。如果有人有更多的改進意見,這將是最受歡迎的(在分辨率方面並沒有多大幫助)

void SceneWidget::showAsPixmap(Plotable::GraphicItems const& items){ 
    QRectF boundingRect; 
    boostForeach(QGraphicsItem* pItem,items) { 
     boundingRect = boundingRect.united(pItem->boundingRect()); 
    } 
    QSize size(boundingRect.size().toSize()); 
    double const cMaxRes =1920; 
    double const scale = cMaxRes/boundingRect.size().width(); 
    QPixmap pixmap(size*scale); 
    pixmap.fill(Qt::transparent); 
    QPainter p(&pixmap); 
    //p.setCompositionMode(QPainter::CompositionMode_Source); 
    p.translate(-boundingRect.topLeft()*scale); 
    p.scale(scale,scale); 
    QStyleOptionGraphicsItem opt; 
    boostForeach(QGraphicsItem* item,items) { 
     item->paint(&p, &opt, 0); 
    } 
    p.end(); 
    QGraphicsPixmapItem* item = new QGraphicsPixmapItem(pixmap); 
    item->scale(1.0/scale,-1.0/scale); 
    item->setOffset(boundingRect.topLeft()*scale); 
    showOnScene(item); 
} 
相關問題