我发现我在自定义 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_ );