setTransformOriginPoint带来高CPU使用率

时间:2013-10-08 09:41:45

标签: qt paint cpu-usage qt4.7

我发现自定义GraphicsItem中的paint函数占用了100%的CPU(在最坏的情况下)

我发现是这样的: setTransformOriginPoint

覆盖所有其他功能(drawRect,设置Hight Quality antialias等)。

有趣的是我删除了它,一切正常。我把它放在翻译之后,所以据说它会旋转与新的变换原点相关。但无论如何它都有用......我想知道为什么......

但主要问题是:为什么100%?

我给你的项目的油漆代码给出了高CPU使用率:

// Translate all to the center of the ruler calculated in the itemChange method.
  painter->translate(rulerCenter_);
  // rotate with the center where the ruler center is
  //setTransformOriginPoint(rulerCenter_); <-- BRINGS 100% USAGE, NO SENSE WHY IT WORKS WITHOUT THIS.

  painter->rotate(rulerRotation_);

  // Set the color for the lines and quality of the lines
  painter->setRenderHint(QPainter::Antialiasing, true);
  painter->setPen(linesColor_);

  // Draw long line of the ruler next to the wall
  painter->drawLine(-length_/2,0,length_/2,0);

  // Lines in the sides
  painter->drawLine(-length_/2, 0, -length_/2, -sideLinesSize_); 
  painter->drawLine(length_/2, 0, length_/2, -sideLinesSize_); 

  // if we should flip the text for the user to read it properly...
  if (flippedText_)
    painter->rotate(180);

  // Prepare for the text box, moving it to be centered
  painter->translate(-textBox_.width()/2,-textBox_.height()/2);

  // draw a box under the text so it hides whatever is under it
  painter->setBrush(textBackgroundColor_);
  painter->setPen(Qt::NoPen);
  painter->drawRect(textBox_);

  // Draw the text
  painter->setPen(pen_);
  painter->setFont(font_);
  painter->setRenderHint(QPainter::HighQualityAntialiasing, true);
  painter->drawText(textBox_, Qt::AlignCenter, meassureString_ );

1 个答案:

答案 0 :(得分:1)

您应该将渲染与所有其他处理任务分开。

绘画功能仅用于绘制项目,而不用于移动或旋转对象。如果您尝试在绘制函数中进行渲染以外的任何操作,则会延迟或停止图形管道。这可能是你调用setTransformOriginPoint时发生的事情。

在渲染之前设置图形管道需要大量处理。如果你拖延管道,它必须再次执行,这将占到100%的处理器时间。

虽然它描述了ARM处理器中发生的事情,theory explained is the same here