c++qtqpainterpath

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.

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.

My current solution has me creating multiple QGraphicsPathItems, each colored differently with their respective QPens. As I get data, I populate the PainterPath associated with those PathItems. 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. 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.

Bearing Formula calculations in GraphicsScene producing erratic results

Here is what I'm trying to do. Desired Result

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; the color needs to flow along the line.

Here is what is happening:

enter image description here

As you can see, the red line (PathItem) jumps from where it last was visible to the new position.

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.


Solution

  • You can use multiple QPainterPath, one for each color. And then paint them all with the proper color. Be sure to use moveTo() to deplace the current path cursor without drawing a line.

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

    render

    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.