2009-07-21 158 views
0

我已經編寫了我自己的QGraphicsView.drawItems()實現,以適應我的應用程序的需求。這樣的方法工作正常,但是,即使不需要重繪,它也會被重複調用。這會導致應用程序最大限度地利用處理器。
我是否需要以某種方式表示繪圖完成?我閱讀了git樹中的源代碼,我看不到任何這樣的事情正在完成。
應用程序是在Python/PyQt的,和我的畫法是這樣的:Qt GraphicsScene不斷重繪

def drawItems(self, painter, items, options): 
    markupColors={'manual':QColor(0, 0, 255), 
        'automatic':QColor(255, 0, 0), 
        'user':QColor(0, 255, 0)} 

    for index in xrange(len(items)): 
     item=items[index] 
     option=options[index] 

     dataAsInt, dataIsInt=item.data(self.DRAWABLE_INDEX).toInt() 
     drawable=None 
     if dataIsInt: 
      drawable=self.drawables[dataAsInt] 
      item.setPen(markupColors[drawable.source]) 
     item.paint(painter, option, None) 

視圖的方法由「猴子補丁」重寫,像這樣:

self.ui.imageArea.drawItems=self.drawer.drawItems 

以上方法是self.drawer.drawItems在最後一個語句中。

任何想法,爲什麼會發生這種情況?

回答

0

我認爲這會導致問題:

item.setPen(markupColors[drawable.source]) 

如果你看看源代碼:

void QAbstractGraphicsShapeItem::setPen(const QPen &pen) 
{ 
    Q_D(QAbstractGraphicsShapeItem); 
    prepareGeometryChange(); 
    d->pen = pen; 
    d->boundingRect = QRectF(); 
    update(); 
} 

它調用update()每次筆設置。

+0

啊,非常好。謝謝! 我將它改爲`painter.setPen`,而不是它改變顏色?我將不得不嘗試設置一次筆。 – carlpett 2009-07-21 15:00:07