2013-05-11 146 views
1

我使用QGraphicsView,QGrapichsSceneQGraphicsItem來繪製一些圖表。我已經實現QGraphicsItem::paint函數來繪製文本(圖表的值),但是它不會在每次必須繪製新東西時被調用。我paint功能QGraphicsItem paint not called

void CLabelItem::paint(QPainter *painter, 
const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget = 0*/) 
{ 

    if (GetValue() < 0) 
    { 
     return; 
    } 
    painter->drawText(boundingRect(), m_value.toString()); 
} 

所以我的問題是 - 爲什麼QGraphicsItem::paint可以不叫,我怎麼可能使它被稱爲?

回答

2

您需要從修改m_value變量觸發重繪的函數調用QGraphicsItem::update()

+0

感謝您的答案,它的工作!但現在又出現了另一個問題 - 所有項目都沒有調用「QGraphicsItem :: paint」,儘管它們都有正確的邊界矩陣。你能幫忙嗎? – nabroyan 2013-05-11 21:36:19

+1

@nabroyan這些項目的邊界扭曲是否在重繪之間改變?因爲在這種情況下(除了'update()'),你需要調用'prepareGeometryChange()'。 – alexisdm 2013-05-11 21:44:02

+0

我明白了,這是計算邊界矩的錯誤。 – nabroyan 2013-05-12 08:15:50

0

一般用Qt繪製的圖形項目是在調用paintEvent時繪製的,然後就可以在paint函數內處理繪畫了。