PDA

View Full Version : How to create sections on a Qwt Plot



dvez43
30th August 2011, 16:58
Here's what I am looking to do, and I haven't really found a solution yet within the Qwt documentation.

I would like to create 2 vertical lines on a Qwt Plot. I already have the plot functionality working as well as being able to plot points on the plot.

So, in between these two lines I would like to change the background color for just that section and only that section. Once the line has been reached, I want the background color before and after the double line section to remain the same as the default background color already set.

Does anyone know if this is this possible?

p.s. the section would need to be behind the curves already created on the QwtPlot, in a way, transparent.

Thanks in advance!

ars
30th August 2011, 18:40
You can create a custom "RectanglePlotItem" class derived from QwtPlotItem and add instances of this class to your plot. It should draw a rectangle in the color of your section. By specifying the z-order you can draw this item behind the curve.

Uwe
31st August 2011, 14:14
Something like this:


class YourItem: public QwtPlotItem
{
public:
YourItem():
m_orientation( Qt::Horizontal ),
m_pen( Qt::red ),
m_brush( Qt::blue )
{
setZ( 0.0 );
}

void setInterval( const QwtInterval &interval )
{
m_interval = interval;
}

void setOrientation( Qt::Orientation orientation )
{
m_orientation = orientation;
}

virtual int rtti() const
{
return QwtPlotItem::Rtti_PlotUserItem;
}

virtual void draw( QPainter *painter,
const QwtScaleMap &xMap, const QwtScaleMap &yMap,
const QRectF &canvasRect ) const
{
if ( !m_interval.isValid() )
return;

const double off = qMax( m_pen.widthF(), 1.0 );

QRectF rect;
if ( m_orientation == Qt::Horizontal )
{
rect.setLeft( canvasRect.left() - off );
rect.setRight( canvasRect.right() + off );
rect.setTop( yMap.transform( m_interval.minValue() ) );
rect.setBottom( yMap.transform( m_interval.maxValue() ) );
}
else
{
rect.setLeft( canvasRect.top() - off );
rect.setRight( canvasRect.bottom() + off );
rect.setLeft( xMap.transform( m_interval.minValue() ) );
rect.setRight( xMap.transform( m_interval.maxValue() ) );
}

rect = rect.normalized();

painter->setPen( m_pen );
painter->setBrush( m_brush );
painter->drawRect( rect );

}

private:
Qt::Orientation m_orientation;
QwtInterval m_interval;
QPen m_pen;
QBrush m_brush;
};
Uwe

Markus_AC
23rd November 2011, 11:19
I'm using something like this, too.
In my application I often need just to change these backgournd-areas; the plot-data stays the same.

So how can I trigger this background draw-funktion without redrawing the whole curve?