【发布时间】:2012-12-13 15:32:07
【问题描述】:
我目前正在尝试将我的 QPainter 对象封装到可重用的类中,可能是相互派生的。这使他们能够以任何他们喜欢的方式改变画家,有自己的孩子来画画等等:
我有 DrawArc 派生自 QPainterPath
DrawArc::DrawArc() : QPainterPath()
{}
void DrawArc::paint(QPainter* painter)
{
painter->save();
//...
arcTo(/*...*/);
lineTo(/*...*/);
painter->translate(QPoint(100,100));
painter->drawPath(*dynamic_cast<QPainterPath*>(this));
painter->restore();
}
和DrawBeam 派生自DrawArc
DrawBeam::DrawBeam() : DrawArc()
{}
void DrawBeam::paint(QPainter* painter)
{
painter->save();
//...
painter->setPen(QPen(color, 4));
painter->setBrush(brush);
DrawArc::paint(painter);
painter->restore();
}
在实际的小部件中,我正在执行以下操作
BeamWidget::BeamWidget(QWidget* parent) : QWidget(parent)
{
DrawBeam* mybeam = new DrawBeam();
}
void BeamWidget::paintEvent(QPaintEvent * /* event */)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
mybeam->paint(&painter);
}
但是,几秒钟(或数百次重绘)后,painter->drawPath(*dynamic_cast<QPainterPath*>(this)); 的性能大幅下降。其余过程中的其他一切似乎都运行良好,但是当我启用该行时,性能会迅速下降。
此外,源自DrawArc 绘画的所有元素有时似乎会失去其 QBrush 样式并保持可见,即使设置了 setAutoFillBackground(true);...
【问题讨论】:
-
Also all elements deriving from DrawArc painting seem to sometimes lose their QBrush styles你正在拯救画家…… -
据我所知,
painter->save()和painter->restore()让我为我的画家拍一张快照,这样我就可以在完成后展开回到它,撤消所有转换并取消选中的钢笔、画笔等。 -
它开启了在绘画过程中使用不同风格的可能性。
-
顺便说一句,性能损失与什么相比?您的帖子中似乎没有其他选择。
-
与程序启动后相比。没有数据被更改,但重复绘制会使连续绘制变慢。