1 Attachment(s)
qwtWidgetOverlay going into left axis scale
I'm trying to use a QwtWidgetOverlay for a rectangle that I'm using to highlight a plot region. The issue I'm having is that the overlay is not disappearing at the left edge of the plot, but being allowed to go all the way to the left edge of the y-axis scale / label.
On the right edge of the plot, the overlay disappears properly at the plot's edge.
https://i.imgur.com/fBZ6h68.png
My code puts a QwtPlotItem (the rectangle) inside of a QwtWidgetOverlay, and I'm using plot->canvas()->contentsRect() to get the canvas boundaries.
Any pointers would be greatly appreciated.
Code:
{
public:
virtual ~RectangleOverlay();
const QRectF &canvasRect
) const override
{
int x1 = qRound(xMap.transform(m_interval.minValue()));
int x2 = qRound(xMap.transform(m_interval.maxValue()));
// Fill color
blue.setAlpha(125);
// Rectangle
pen.setColor(pen.color().lighter());
pen.setWidth(m_edgeWidth);
painter->setPen(pen);
// Add penWidth's pixels to the top and bottom of the rectangle
// so only the side lines are visible
QRect overlayRect
(x1, canvasRect.
top()-m_edgeWidth, x2
- x1, canvasRect.
height()+m_edgeWidth
);
painter->drawRect(overlayRect);
painter->fillRect(overlayRect, brush);
}
virtual QRectF boundingRect
() const override
{
int x1 = qRound(xMap.transform(m_interval.minValue()));
int x2 = qRound(xMap.transform(m_interval.maxValue()));
QRectF overlayRect
(x1
- m_edgeWidth,
m_plot->canvas()->contentsRect().top(),
x2 - x1 + (2*m_edgeWidth),
m_plot->canvas()->contentsRect().height());
return overlayRect;
}
void setInterval(QwtInterval &interval) {m_interval = interval;}
QwtInterval getInterval() const {return m_interval;}
protected:
private:
QwtInterval m_interval;
int m_edgeWidth;
};
class Overlay: public QwtWidgetOverlay
{
public:
: QwtWidgetOverlay(parent)
, m_plot
(dynamic_cast<const
QwtPlot*>
(parent
)) {
}
virtual ~Overlay() {};
void attachPlotItem
(QwtPlotItem *plotItem
) {m_plotItem
= plotItem;
}
protected:
virtual void drawOverlay
( QPainter *painter
) const {
if (!m_plot)
return;
const QwtScaleMap xMap
= m_plot
->canvasMap
(m_plotItem
->xAxis
());
const QwtScaleMap yMap
= m_plot
->canvasMap
(m_plotItem
->yAxis
());
QRectF rect
(m_plot
->canvas
()->contentsRect
());
if (m_plotItem)
m_plotItem->draw(painter, xMap, yMap, m_plot->canvas()->contentsRect());
}
{
if (!m_plot)
QRectF boundingRect
= m_plotItem
->boundingRect
();
return QRegion(boundingRect.
x(), boundingRect.
y(), boundingRect.
width(), boundingRect.
height());
}
private:
};
Re: qwtWidgetOverlay going into left axis scale
Use the plot canvas as parent.
HTH,
Uwe
Re: qwtWidgetOverlay going into left axis scale
Thanks, Uwe, that fixed the bounding issue!
However, a quick use-model question on QwtWidgetOverlay.
I notice that in your Editor example you're attaching the QwtPlotItem directly to the canvas and then creating and destroying a temporary QwtWidgetOverlay when the item is moved.
Is this the intended use-model?
In my implementation, I never attach() the QwtPlotItem to the plot. I just manually call updateOverlay() whenever the overlay is dragged or moved or replot() is called.
This seems to work fine but I'm wondering if I'm doing it wrong.
Re: qwtWidgetOverlay going into left axis scale
Well, an overlay is not part of what is seen by QwtPlotRenderer.
Uwe