2011-06-05 17 views
3

我有一個從QGraphicsLineItem繼承的類,只要我重寫paint方法,它看起來像Qt開始在每個「主循環」繪製它,而不是繪製根據到某些事件(如移動項目等)。覆蓋QGraphicsLineItem :: paint()時100%的CPU使用率()

有沒有人知道更多關於從QGraphicsItem繼承的良好做法?我看着其他projets的代碼,它看起來不是來自我的繪畫方法。我在想,也許我在paint方法中做了一些錯誤,將項目狀態更改爲「再次繪製」,所以Qt再次繪製它。我加入了方法代碼。該方法繪製一個箭頭。

void Message::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) 
{ 
    QLineF line = this->line(); 
    Instance* from = dynamic_cast<Instance*> (this->from_get()); 
    Instance* to = dynamic_cast<Instance*> (this->to_get()); 

    QPointF from_pt(from->x() + from_pos_.x(), from->y() + from_pos_.y()); 
    line.setP1(from_pt); 
    this->setLine(line); 

    QPointF to_pt(to->x() + to_pos_.x(), to->y() + to_pos_.y()); 
    line.setP2(to_pt); 
    this->setLine(line); 

    textItem_->setPos(this->boundingRect().center().x() - textItem_->boundingRect().width()/2, 
        this->boundingRect().center().y() - textItem_->boundingRect().height()/2); 
    rectItem_->setRect(textItem_->x(), textItem_->y(), textItem_->boundingRect().width(), textItem_->boundingRect().height()); 

    if (this->line().dy() >= 0) 
    { 
    int arrowSize = 14; 
    double angle = ::acos(this->line().dx()/this->line().length()); 
    QPointF arrowP1; 
    QPointF arrowP2; 
    QPolygonF p; 

    angle = (Pi * 2) - angle; 
    arrowP1 = this->line().p2() - QPointF(sin(angle + Pi/3) * arrowSize, cos(angle + Pi/3) * arrowSize); 
    arrowP2 = this->line().p2() - QPointF(sin(angle + Pi - Pi/3) * arrowSize, cos(angle + Pi - Pi/3) * arrowSize); 
    p << this->line().p2() << arrowP1 << arrowP2; 
    extremity_->setPolygon(p); 
    extremity_->update(extremity_->boundingRect()); 
    } 

    extremity_->paint(painter, option, widget); 

    QGraphicsLineItem::paint(painter, option, widget); 
} 

謝謝你的幫忙!

回答

4

你可能不應該打電話給你的paint()方法裏面像setPossetRectsetPolygonupdate方法。這些方法可能會安排一個新的繪圖事件,這將導致無限遞歸。

+0

這是來自更新(),我剛剛刪除該行,它現在確定。謝謝。 – 2011-06-06 08:22:26