简体   繁体   English

派生QPainterPath,QPainter性能迅速下降

[英]Derived QPainterPath, QPainter performance degrading quickly

I am currently trying to encapsulate my QPainter objects into reusable classes, possibly deriving each other. 我目前正在尝试将我的QPainter对象封装为可重用的类,可能是彼此派生的。 This enables them to transform the painter around any way they like, have their own children to draw etc: 这使他们可以按照自己喜欢的任何方式变换画家,让自己的孩子画画,等等:

I have DrawArc derived from QPainterPath 我有从QPainterPath派生的DrawArc

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();
}

and DrawBeam derived from DrawArc 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();
}

In the actual Widget I am doing the following 在实际的小部件中,我正在执行以下操作

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);
}

However I am seeing dramatic performance losses in painter->drawPath(*dynamic_cast<QPainterPath*>(this)); 但是我在painter->drawPath(*dynamic_cast<QPainterPath*>(this));看到了巨大的性能损失painter->drawPath(*dynamic_cast<QPainterPath*>(this)); after a few seconds (or few hundred redraws). 几秒钟后(或几百次重绘)。 Everything else in the remaining procedure seems to run fine but when I enable that line the performance degrades quickly. 其余过程中的所有其他内容似乎运行良好,但是当我启用该行时,性能会迅速下降。

Also all elements deriving from DrawArc painting seem to sometimes lose their QBrush styles and remain visible even though setAutoFillBackground(true); 同样,即使setAutoFillBackground(true);DrawArc绘画派生的所有元素似乎有时也会丢失其QBrush样式,并保持可见setAutoFillBackground(true); is set... 设置...

I found out this has to do with me only creating the object once, then adding an arcTo and a few other lines to it during each run of paint() . 我发现这与只创建对象一次有关,然后在每次paint()运行期间向其中添加一个arcTo和其他几行。 Since I can't flush the QPainterPath the Path simply becomes longer and longer and longer and longer. 由于我无法刷新QPainterPath,因此路径变得越来越长。

This explains why old lines are not being flushed and why the brush is alternating (each time I re-draw the same thing I am forming a new intersection with the path itself, wich by design is not being filled). 这就解释了为什么不冲洗旧线条以及为什么画笔交替使用(每次我重新绘制相同的东西时,我都会与路径本身形成一个新的相交点,而不会被设计填充)。

I fixed it like this: 我这样修复:

void DrawArc::paint(QPainter* painter)
{
    painter->save();
    //...

    QPainterPath path = QPainterPath(*this);
    path.arcTo(/*...*/);
    path.lineTo(/*...*/);

    painter->translate(QPoint(100,100));
    painter->drawPath(path);
    painter->restore();
}

So in each drawing operation I create a copy of the current path, add all lines I need to it and paint that one. 因此,在每个绘制操作中,我都会创建当前路径的副本,在其中添加所需的所有线条并绘制该路径。 After exiting paint , that drawn path is being discarded. 退出paint ,该绘制的路径将被丢弃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM