Results 1 to 5 of 5

Thread: How to set boundary for QwtPlotMarker ?

  1. #1
    Join Date
    Jun 2015
    Posts
    109
    Thanks
    20
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default 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

  2. #2
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default 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

  3. #3
    Join Date
    Jun 2015
    Posts
    109
    Thanks
    20
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    Qt Code:
    1. class Line: public QwtPlotMarker
    2. {
    3. public:
    4. Line(QwtText label, const QPointF value, Qt::Orientation orientation,
    5. LineStyle style, QPen pen = QPen(Qt::blue, 1, Qt::DashDotLine)) :
    6. {
    7. setSymbol(sym);
    8. setLabel(label);
    9. setTitle(label);
    10. setValue(value);
    11. setLabelOrientation(orientation);
    12. setLinePen(pen);
    13. setLineStyle(style);
    14. setLabelAlignment(Qt::AlignRight|Qt::AlignBottom);
    15. setItemAttribute(QwtPlotItem::Legend, true);
    16. setRenderHint(QwtPlotItem::RenderHint::RenderAntialiased);
    17. setLegendIconSize(QSize(20,30));
    18. }
    19. QwtSymbol *sym = new QwtSymbol(QwtSymbol::Diamond,QBrush(Qt::red),QPen(Qt::red),QSize(5,5));
    20. };
    To copy to clipboard, switch view to plain text mode 
    I have created vertical line and set position on left side say, 200 and on right side say, 800,
    Qt Code:
    1. m_line[0] = new Line(QwtText("VLine1"), QPointF(200,0), Qt::Vertical, QwtPlotMarker::VLine);
    2. m_line[0]->attach(m_plot);
    3. m_line[1] = new Line(QwtText("VLine2"), QPointF(800,0), Qt::Vertical, QwtPlotMarker::VLine);
    4. m_line[1]->attach(m_plot);
    To copy to clipboard, switch view to plain text mode 
    Now on scroll (wheel) event, I have setting it to the border of the widget,
    Qt Code:
    1. bool OscilloscopeWidget::eventFilter(QObject *object, QEvent *e)
    2. {
    3. switch(e->type())
    4. {
    5. case QEvent::Wheel:
    6. m_line[0]->setValue(QPoint(m_plot->invTransform(QwtPlot::xBottom, m_plot->transform(QwtPlot::xBottom, m_grid->xScaleDiv().interval().minValue())+20),0));
    7. m_line[1]->setValue(QPoint(m_plot->invTransform(QwtPlot::xBottom, m_plot->transform(QwtPlot::xBottom, m_grid->xScaleDiv().interval().maxValue())-20),0));
    8. break;
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    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 ?

  4. #4
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to set boundary for QwtPlotMarker ?

    Remove your event filter and try something like this:

    Qt Code:
    1. YourPlot::YourPlot( ... )
    2. {
    3. connect( axisWidget( xBottom ), &QwtScaleWidget::scaleDivChanged, this, &YourPlot::alignMarkers );
    4. }
    5.  
    6. void YourPlot::alignMarkers()
    7. {
    8. const double margin = ...; // pixels
    9.  
    10. const auto map = canvasMap( xBottom );
    11.  
    12. const double min = map.invTransform( map.p1() + margin );
    13. const double max = map.invTransform( map.p2() - margin );
    14.  
    15. m_line[0]->setXValue( qMax( m_line[0]->xValue(), min ) );
    16. m_line[1]->setXValue( qMin( m_line[1]->xValue(), max ) );
    17. }
    To copy to clipboard, switch view to plain text mode 
    Or even better:

    Qt Code:
    1. class YourMarker: public QwtPlotMarker
    2. {
    3. public:
    4. YourMarker( ... )
    5. {
    6. setItemAttribute( QwtPlotItem::ScaleInterest, true );
    7. }
    8.  
    9. void updateScaleDiv( const QwtScaleDiv& xMap, const QwtScaleDiv& ) override
    10. {
    11. const double margin = ...; // pixels
    12.  
    13. const double min = xMap.invTransform( xMap.p1() + margin );
    14. const double max = xMap.invTransform( xMap.p2() - margin );
    15.  
    16. setXValue( qBound( min, xValue(), max ) );
    17. }
    18. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by Uwe; 16th January 2019 at 09:45.

  5. The following user says thank you to Uwe for this useful post:

    npatil15 (17th January 2019)

  6. #5
    Join Date
    Jun 2015
    Posts
    109
    Thanks
    20
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default 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
    Qt Code:
    1. m_line[0]->setValue(QPoint(m_plot->invTransform(QwtPlot::xBottom, m_plot->transform(QwtPlot::xBottom, m_grid->xScaleDiv().interval().minValue())+20)+1,0));
    To copy to clipboard, switch view to plain text mode 


Similar Threads

  1. Replies: 2
    Last Post: 20th July 2015, 07:14
  2. QPushButton with blinking boundary
    By giovanni.foiani in forum Newbie
    Replies: 2
    Last Post: 9th August 2012, 11:47
  3. How can we set view boundary
    By learning_qt in forum Qt Programming
    Replies: 3
    Last Post: 21st November 2008, 08:16
  4. Replies: 1
    Last Post: 5th August 2008, 14:53
  5. How to fix the boundary..??
    By deepusrp in forum Qt Programming
    Replies: 18
    Last Post: 17th May 2007, 05:41

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.