简体   繁体   English

如何以不同方式为QPainterPath子路径着色?

[英]How can I color QPainterPath subpaths differently?

As the title suggests, I'm looking for a way to color the subpaths of a QPainterPath different colors which is applied to a QGraphicsPathItem, OR simply change the color along the PathItem with a QGradient QPen. 正如标题所示,我正在寻找一种方法来为QPainterPath的不同颜色的子路径着色,这些颜色应用于QGraphicsPathItem,或者只是使用QGradient QPen沿PathItem更改颜色。

Ultimately I am trying to find the correct solution which will give me the ability to visibly draw a single line, which changes colors based on external variables. 最终我试图找到正确的解决方案,这将使我能够明显地绘制一条线,根据外部变量改变颜色。

I am using a QGraphicsScene to draw everything. 我正在使用QGraphicsScene来绘制所有内容。

My current solution has me creating multiple QGraphicsPathItems, each colored differently with their respective QPens. 我目前的解决方案让我创建了多个QGraphicsPathItems,每个QGraphicsPathItems的颜色与各自的QPens不同。 As I get data, I populate the PainterPath associated with those PathItems. 当我获取数据时,我填充与那些PathItems相关联的PainterPath。 This gives me the multicolored lines I need, but the lines are visibly disconnected. 这给了我需要的多彩线条,但线条明显断开。

I either need to be able to make subpaths of the QPainterPath invisible during the color change, or alter the gradient applied to a single PathItem. 我需要能够在颜色更改期间使QPainterPath的子路径不可见,或者更改应用于单个PathItem的渐变。 Or maybe there is another approach I am missing. 或者也许还有另一种我想念的方法。 Any help would be wonderful. 任何帮助都会很精彩。

-Edit: -编辑:

This is how I am currently doing the drawing, as noted in the solution to my question. 正如我的问题的解决方案中所述,这就是我目前正在进行绘图的方式。 Again, note that I am using a GraphicsScene. 再次注意,我正在使用GraphicsScene。

Bearing Formula calculations in GraphicsScene producing erratic results GraphicsScene中的方位公式计算产生不稳定的结果

Here is what I'm trying to do. 这就是我想要做的。 期望的结果

As you can see the line changes color as it is drawn, by external variables. 正如您所看到的那样,线条在外部变量绘制时会改变颜色。 I'm afraid a Qgradient might not work because the line will not always be straight; 我担心Qgradient可能不起作用,因为线条并不总是笔直的; the color needs to flow along the line. 颜色需要沿着线条流动。

Here is what is happening: 以下是发生的事情:

在此输入图像描述

As you can see, the red line (PathItem) jumps from where it last was visible to the new position. 如您所见,红线(PathItem)从最后可见的位置跳到新位置。

To help better clarify the behavior, imagine that this line is being drawn over time. 为了更好地澄清行为,想象一下这条线是随着时间的推移而绘制的。 It starts out red, soon a variable gets set and the color of the line segments being drawn change to orange. 它从红色开始,很快变量被设置并且绘制的线段的颜色变为橙色。 The red portions of the line remain intact so we can see historically what the state of the variable was at that time. 该行的红色部分保持不变,因此我们可以从历史上看到当时该变量的状态。 At different times, the variable adjusts and the color applied to new portions of the line update accordingly. 在不同的时间,变量调整并且应用于线的新部分的颜色相应地更新。

When the line has finished drawing, we can look at it and see when the colors changed. 当线条完成绘制后,我们可以查看它并查看颜色何时发生变化。

I hope this all makes sense. 我希望这一切都有道理。

You can use multiple QPainterPath , one for each color. 您可以使用多个QPainterPath ,每种颜色一个。 And then paint them all with the proper color. 然后用适当的颜色涂上它们。 Be sure to use moveTo() to deplace the current path cursor without drawing a line. 务必使用moveTo()来取代当前路径光标而不绘制线条。

void Widget::paintEvent(QPaintEvent *event)
{
    QPainterPath redPath;
    QPainterPath bluePath;
    redPath.moveTo(0,0);
    redPath.lineTo(60,60);
    bluePath.moveTo(60,60);
    bluePath.lineTo(70,20);
    redPath.moveTo(70,20);
    redPath.lineTo(100,100);
    bluePath.moveTo(100,100);
    bluePath.lineTo(160,260);


    QPainter painter(this);
    painter.setRenderHint(QPainter::HighQualityAntialiasing, true);
    painter.setPen(QPen(Qt::red, 4));
    painter.drawPath(redPath);
    painter.setPen(QPen(Qt::blue, 4));
    painter.drawPath(bluePath);

}

给予

If you feel the calls to "moveTo" and "lineTo" are a bit heavy, you can encapsulate all the QPainterPaths in you own class with a lineTo(QPointF, QColor) function that will handle the path switching when you change the color. 如果您觉得对“moveTo”和“lineTo”的调用有点沉重,您可以使用lineTo(QPointF, QColor)函数将所有QPainterPath封装在您自己的类中,该函数将在您更改颜色时处理路径切换。

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

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