[Qt-interest] drawing polyline with QPainterPath
Sean Harmer
sean.harmer at maps-technology.com
Tue Jun 2 10:13:25 CEST 2009
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
More information about the Qt-interest-old
mailing list