100% CPU usage when overriding QGraphicsLineItem :: paint ()

I have a class that inherits from QGraphicsLineItem, and as soon as I redefine the drawing method, it looks like Qt starts to draw it in each “main loop”, instead of drawing according to some events (for example, moving an element, etc.).

Does anyone know more about good practices for inheriting from QGraphicsItem? I am looking at other projets code and it looks like this is not from my drawing method. I thought that maybe I'm doing something wrong in the drawing method, which changes the states of the elements to "draw again", and so Qt draws again. I joined the method in the case. The method draws an arrow.

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);
}

Thank you for your help!

+3
source share
1

, setPos, setRect, setPolygon update paint(). , .

+4

All Articles