qt-interest@trolltech.com
[Top] [All Lists]

Re: [Qt-interest] drawing polyline with QPainterPath

Subject: Re: [Qt-interest] drawing polyline with QPainterPath
From: Sean Harmer
Date: Tue, 2 Jun 2009 09:13:25 +0100
Hi,

On Tuesday 02 Jun 2009 08:56:43 Jan wrote:
> Hi,
>
> I have subclassed QGraphicsPathItem to draw a polyline in my
> graphicsview. This is the code:
>
>       QPainterPath path;
>
>       path.addPolygon(m_line); //m_line is a non closed polygon
>       path.connectPath(path.toReversed());
>       setPath(path);
>
> in paint() function:
>
>       painter->drawPolyline(m_line);
>
> The problem is, that it is always treated as an area i.e. last and first
> point are connected which is visible when I hover the area with the
> cursor. My "workaround" was this :
>
>       path.connectPath(path.toReversed());
>
> I add the same polyline in a reversed order and don't use
> painter->drawPath(path()) in the paint function.
>
> How can I draw a polyline with QPainterPath that is not connected
> (first, last point) and therefore has no area without this "reversed"
> thing?
This kind of construct works for me:

QVector<QPointF> pPrime;
m_coordSystem->mapToPrimed( m_dataSet, pPrime );

QPainterPath path;
if ( !m_fillPath )
{
    path.moveTo( pPrime.at( 0 ) );
    for ( int i = 1; i < pPrime.size(); ++i )
        path.lineTo( pPrime.at( i ) );
}
else
{
    // We need to construct a closed path
    QPointF zero( 0.0, 0.0 );
    QPointF zeroPrime = m_coordSystem->mapToPrimed( zero );
    path.moveTo( pPrime.at( 0 ).x(), zeroPrime.y() );
    for ( int i = 0; i < pPrime.size(); ++i )
        path.lineTo( pPrime.at( i ) );
    path.lineTo( pPrime.last().x(), zeroPrime.y() );
    path.closeSubpath();
}

then later I simply call QPainter::drawPath(). You can ignore my m_coordSystem 
object it simply transforms my data set into a more convenient coordinate 
system.

HTH,

Sean

_______________________________________________
Qt-interest mailing list
Qt-interest@xxxxxxxxxxxxx
http://lists.trolltech.com/mailman/listinfo/qt-interest

<Prev in Thread] Current Thread [Next in Thread>