100%的CPU使用率覆盖QGraphicsLineItem :: paint() [英] 100% CPU usage when overriding QGraphicsLineItem::paint()

查看:387
本文介绍了100%的CPU使用率覆盖QGraphicsLineItem :: paint()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个继承自QGraphicsLineItem的类,只要我重写paint方法,它看起来像Qt开始绘制它在每个主循环,而不是根据一些事件(如移动项目等)绘制。



有人知道更多关于从QGraphicsItem继承时的良好做法吗?我看看其他projets的代码,它看起来像不是从我的paint方法。我想,也许我在做一些错误的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 ;

if(this-> line()。dy()> = 0)
{
int arrowSize = 14;
double angle = :: acos(this-> line()。dx()/ this-> line()。
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);
}

感谢您的帮助!

setPos , setRect paint()内有 setPolygon 更新方法。这些方法很可能安排一个新的paint事件,这将导致无限递归。


I have a class inheriting from QGraphicsLineItem and as soon as I override the paint method, it looks like Qt starts painting it at every "main loop", instead of drawing according to some events (like moving the item, etc).

Does anyone know more about the good practices when inheriting from a QGraphicsItem ? I look at other projets' code and it looks like it does not come from my paint method. I was thinking that maybe I'm doing something wrong in the paint method that changes the items states to "to be painted again", and so Qt paints it again. I joined the method code in case of. The method paints 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 !

解决方案

You probably shouldn't call methods like setPos, setRect, setPolygon or update inside your paint() method. These methods are likely to schedule a new paint event which will lead to infinite recursion.

这篇关于100%的CPU使用率覆盖QGraphicsLineItem :: paint()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆