0

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

我发现是这样的:setTransformOriginPoint

超过所有其他功能(drawRect、设置高质量抗锯齿等)。

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

但主要问题是:为什么是 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_ );
4

1 回答 1

1

You should be separating rendering from all other processing tasks.

The paint function is just for painting an item, not for moving or rotating objects. If you try to do anything other than rendering in the paint function, you'll either delay or stall the graphics pipeline. This is likely to be what is happening when you call setTransformOriginPoint.

Setting up the graphics pipeline before rendering takes a considerable amount of processing. If you stall the pipeline, it has to do it again, which would account for the 100% processor time.

Although it describes what's happening in an ARM processor, the theory explained is the same here.

于 2013-10-08T09:59:38.737 回答