QPainterPath with variable line width



  • Hi all,
    I have a QPainterPath that I need to draw in a QWidget.

    My actual code is:
    @
    void MyWidget::paintEvent(QPaintEvent *e)
    {
    QPainter painter(this);
    painter.drawPath(m_path);
    }
    @

    This way I get a line with constant line width. Is there a way to draw the line with a width value per point?



  • You need to draw points individually/separately. When you are drawing you need to set the appropriate width of pen.



  • [quote author="Dheerendra" date="1406217904"]You need to draw points individually/separately. When you are drawing you need to set the appropriate width of pen. [/quote]

    But I need a continuous line...


  • Lifetime Qt Champion

    Hi,

    Just to be sure I'm understanding you right, you would like each point of your path to have a different width ?



  • Hi,
    yes, suppose you have a touchscreen and you want to draw a path with a pen width depending on you Stylus pressure.

    I have a series of X,Y and pressure (an integer from 0 to 500).

    I want a line very similar on what happens in real word where if you press more in the paper you got a stronger line.



  • Got it. You draw point everytime with setting penWidth. This is the only way.



  • [quote author="Dheerendra" date="1406257334"]Got it. You draw point everytime with setting penWidth. This is the only way.[/quote]

    Thanks but it isn't what I need.
    If I move the pointer in the touchscreen very fast I get some space between consecutive points so I haven't a "line".



  • LineWidth is probably not a solution for what you need.
    In effect, you need to draw polygons, not lines. That way, you have full control on how wide they are in each segment. You can even create "lines" that start wide at one end, and become narrow at the other end, and so forth.



  • Thanks,
    now I'm trying with drawLine or drawPolyline but I can't find how to set variable line width...
    You told me this:
    [quote]
    You can even create "lines" that start wide at one end, and become narrow at the other end
    [/quote]
    Are you sure it's possible?

    Thanks



  • Yes, you draw a polygon that's wide at one end, and narrow at the other. It's just a matter of the point coordinates to get the effect you want.

    Something like this:
    @QPolygonF polygon;
    polygon << QPointF(0.0, 0.0) << QPointF(100.0, 3.0) << QPointF(100.0, 6.0) << QPointF(0.0, 9.0);@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.