How to set boundary for QwtPlotMarker ?
Hi Everyone,
I have an application of Oscilloscope where I have 2 vertical and 2 Horizontal lines,
This lines can be drag and drop either vertically or horizontally.
I have used QwtPlotMarker to plot lines and QwtPlotPicker to pick the line and on mouseRelease event drop the line.
Now everything is working fine, but I want to set boundary for this lines to move. As this lines should not move outside of the widget.
Any idea how I can do that ?
For reference please check the below post where I mention my logic implementation for dragging and dropping the line
https://www.qtcentre.org/threads/699...ecting-zooming
Thanks in advance :)
Re: How to set boundary for QwtPlotMarker ?
You can connect to the QwtScaleWidget::scaleDivChanged() signal, where you can move the position of the marker, when being outside.
But this somehow raises the question if your marker is related to a plot position or to a widget ( = plot canvas ) position. If it is only about drawing a line at a certain widget position it might be worth to implement your own type of plot item. F.e. have a look at https://sourceforge.net/p/qwt/code/H...upiemarker.cpp. In case of a simple line the draw method would be not much more than a QPainter::drawLine call - the whole class would be < 50 lines of code.
Uwe
Re: How to set boundary for QwtPlotMarker ?
Yes these lines are related to plot position, but now I have transform line position into widget pixel format.
I have tried with some solution but its acting weird.
I have one structure below to set/create line using QwtPlotMarker,
Code:
{
public:
LineStyle style,
QPen pen
= QPen(Qt
::blue,
1, Qt
::DashDotLine)) : {
setSymbol(sym);
setLabel(label);
setTitle(label);
setValue(value);
setLabelOrientation(orientation);
setLinePen(pen);
setLineStyle(style);
setLabelAlignment(Qt::AlignRight|Qt::AlignBottom);
setRenderHint
(QwtPlotItem::RenderHint::RenderAntialiased);
setLegendIconSize
(QSize(20,
30));
}
};
I have created vertical line and set position on left side say, 200 and on right side say, 800,
Code:
m_line[0]->attach(m_plot);
m_line[1]->attach(m_plot);
Now on scroll (wheel) event, I have setting it to the border of the widget,
Code:
{
switch(e->type())
{
m_line
[0]->setValue
(QPoint(m_plot
->invTransform
(QwtPlot::xBottom, m_plot
->transform
(QwtPlot::xBottom, m_grid
->xScaleDiv
().
interval().
minValue())+20),
0));
m_line
[1]->setValue
(QPoint(m_plot
->invTransform
(QwtPlot::xBottom, m_plot
->transform
(QwtPlot::xBottom, m_grid
->xScaleDiv
().
interval().
maxValue())-20),
0));
break;
}
}
I have set offset of 20 just to keep line within the widget not on the border.
On Zoom, the Right line is working fine and stays at Right border and on deep zoom out comes into center(at 500)(No problem).
The Left line on zoom out stays on the border(until scale on the border is near 498) but after some time gets vanishes behind the widget.
Why the Left one is hidden behind the widget, why its not set center on deep zoom out ?
what I'm missing here, as Right line working fine but not Left one ?
Re: How to set boundary for QwtPlotMarker ?
Remove your event filter and try something like this:
Code:
YourPlot::YourPlot( ... )
{
connect( axisWidget( xBottom ), &QwtScaleWidget::scaleDivChanged, this, &YourPlot::alignMarkers );
}
void YourPlot::alignMarkers()
{
const double margin = ...; // pixels
const auto map = canvasMap( xBottom );
const double min = map.invTransform( map.p1() + margin );
const double max = map.invTransform( map.p2() - margin );
m_line[0]->setXValue( qMax( m_line[0]->xValue(), min ) );
m_line[1]->setXValue( qMin( m_line[1]->xValue(), max ) );
}
Or even better:
Code:
{
public:
YourMarker( ... )
{
}
void updateScaleDiv( const QwtScaleDiv& xMap, const QwtScaleDiv& ) override
{
const double margin = ...; // pixels
const double min = xMap.invTransform( xMap.p1() + margin );
const double max = xMap.invTransform( xMap.p2() - margin );
setXValue( qBound( min, xValue(), max ) );
}
};
Re: How to set boundary for QwtPlotMarker ?
Thanks Uwe,
It helps and added a knowledge.
Where as I have added small change in my code and it works.
As the left line has issue while working, so I have added +1 to the scale which add every time and not get hidden on deep scroll out
Code:
m_line
[0]->setValue
(QPoint(m_plot
->invTransform
(QwtPlot::xBottom, m_plot
->transform
(QwtPlot::xBottom, m_grid
->xScaleDiv
().
interval().
minValue())+20)+1,
0));
:cool: