Results 1 to 13 of 13

Thread: Custom symbol for QwtPlotMarker

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #5
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    20
    Thanked 28 Times in 27 Posts

    Default Re: Custom symbol for QwtPlotMarker

    Thanks Uwe, works like a charm .
    Anyone interested here's a trivial example:
    Step 1 : subclass QwtSymbol
    Qt Code:
    1. class Symbol:public QwtSymbol
    2. {
    3. public:
    4. Symbol(const QBrush &, const QPen &, const QSize & );
    5. virtual void drawSymbols( QPainter *, const QPointF *, int numPoints ) const;
    6. void drawSymbol( QPainter *, const QPointF & ) const;
    7. };
    8.  
    9. Symbol::Symbol(const QBrush &br , const QPen &p, const QSize &sz)
    10. : QwtSymbol(QwtSymbol::NoSymbol, br, p, sz)
    11. {
    12. }
    13.  
    14. void Symbol::drawSymbol(QPainter *painter, const QPointF &pos) const
    15. {
    16. drawSymbols( painter, &pos, 1 );
    17. }
    18.  
    19. void Symbol::drawSymbols(QPainter *painter, const QPointF *points, int numPoints) const
    20. {
    21. if ( numPoints <= 0 )
    22. return;
    23.  
    24. painter->save();
    25. painter->setBrush( brush() );
    26. painter->setPen( pen() );
    27. const QSize sz = size();
    28. const int sw = sz.width();
    29. const int sh = sz.height();
    30. const int sw2 = sz.width() / 2;
    31. const int sh2 = sz.height() / 2;
    32. QPointF pos = points[0];
    33.  
    34. path.moveTo(pos);
    35. path.lineTo(pos.x()-sw2,pos.y()); path.lineTo(pos.x(),pos.y()+sh2); path.lineTo(pos);
    36. painter->drawPath(path);
    37. painter->restore();
    38. }
    To copy to clipboard, switch view to plain text mode 

    Step 2: Subclass QwtMarker
    Qt Code:
    1. class Marker:public QwtPlotMarker
    2. {
    3. public:
    4. Marker();
    5. void drawAt( QPainter *, const QRectF &, const QPointF & ) const;
    6. virtual void draw( QPainter *p, const QwtScaleMap &xMap, const QwtScaleMap &yMap,
    7. const QRectF & ) const;
    8. };
    9.  
    10. Marker::Marker() :QwtPlotMarker()
    11. {}
    12.  
    13. void Marker::drawAt(QPainter *painter, const QRectF &rect, const QPointF &pos) const
    14. {
    15. symbol().drawSymbol(painter, pos);
    16. QwtPlotMarker::drawAt(painter, rect, pos);
    17. }
    18. void Marker::draw( QPainter *painter,
    19. const QwtScaleMap &xMap, const QwtScaleMap &yMap,
    20. const QRectF &canvasRect ) const
    21. {
    22. const double x = xMap.transform( xValue() );
    23. const double y = yMap.transform( yValue() );
    24.  
    25. drawAt( painter, canvasRect, QPointF( x, y ) );
    26. }
    To copy to clipboard, switch view to plain text mode 
    Step 3: Instantiate and use
    Qt Code:
    1. ...
    2. ...
    3. Marker *mrk = new Marker();
    4. mrk->setLinePen(QPen(Qt::black, 0, Qt::DashDotLine));
    5. mrk->setValue(10.0,10.0);
    6. mrk->setLineStyle(QwtPlotMarker::HLine);
    7. mrk->setSymbol(new Symbol(QColor(Qt::yellow), QColor(Qt::green), QSize(15,15)));
    8. mrk->attach(qwt_plot_object);
    To copy to clipboard, switch view to plain text mode 

  2. The following user says thank you to pkj for this useful post:

    sonulohani (24th May 2012)

Similar Threads

  1. QwtPlotMarker example
    By banita in forum Qwt
    Replies: 1
    Last Post: 24th May 2012, 13:19
  2. Replies: 1
    Last Post: 22nd June 2010, 19:56
  3. QwtPlotMarker confusion
    By baray98 in forum Qwt
    Replies: 3
    Last Post: 20th July 2008, 09:47
  4. Replies: 5
    Last Post: 18th March 2008, 09:44

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
  •  
Qt is a trademark of The Qt Company.