Re: Draw text on top of both canvas and plot background?
Brought to you by:
rathmann
|
From: Uwe R. <Uwe...@ti...> - 2019-03-12 09:59:16
|
> I'm attaching a picture where I've made the canvas background blue to
> make it clear. The image shows my attempt with overriding paintEvent,
> note how the "g" is cropped by the canvas which is painted over it.
You have 2 options:
a) Disable the top axis and add a QwtPlotScaleItem instead.
This way the axis would be inside the canvas and you could simply
overload the label method like posted before without losing any space.
But I'm not sure about this solution as it has a couple of problems, you
might not want to accept.
b) Add an extra widget for unit text
If you want to draw on top of canvas and scales you need to add an
additional child widget that is on top of the others:
class UnitLabel : public QWidget
{
public:
UnitLabel( QwtPlot* plot )
: public QWidget( plot )
{
setAttribute( Qt::WindowTransparentForInput, true );
plot->installEventFilter( this );
adjustSize();
}
protected:
void paintEvent( QPaintEvent *event ) override
{
const auto plot = qobject_cast< const QwtPlot* >( parent() );
const auto scaleWidget = plot->axisWidget( QwtPlot::yTop )
QPoint pos = scaleWidget->...
pos = mapFrom( scaleWidget, pos );
QPainter painter;
painter.setPen( ... );
painter.setFont( ... );
painter.drawText( pos, ... );
}
bool eventFilter(QObject *object, QEvent *event) override
{
if ( object == parent() && event->type() == QEvent::Resize )
adjustSize();
return QWidget::eventFilter();
}
private:
void adjustSize()
{
setSize( parentWidget()->size() );
}
};
You will also have to write some code for QwtPlotRenderer - but there
you don't have any issues with clipping.
Uwe
|