Results 1 to 13 of 13

Thread: Custom symbol for QwtPlotMarker

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

    Default Custom symbol for QwtPlotMarker

    1. Subclass QwtSymbol
    Qt Code:
    1. class Symbol:public QwtSymbol
    2. {
    3. public:
    4. Symbol(const QBrush &, const QPen &, const QSize & ): QwtSymbol(QwtSymbol::NoSymbol, br, p, sz)
    5. void drawSymbol( QPainter *, const QPointF & ) const
    6. {
    7. painter->setBrush( brush() );
    8. painter->setPen( pen() );
    9.  
    10. const QSize sz = size();
    11. const int sw = sz.width();
    12. const int sh = sz.height();
    13. const int sw2 = sz.width() / 2;
    14. const int sh2 = sz.height() / 2;
    15.  
    16.  
    17. path.moveTo(pos);
    18. path.lineTo(pos.x()-sw2,pos.y()); path.lineTo(pos.x(),pos.y()+sh2); path.lineTo(pos);
    19. painter->drawPath(path);
    20. }
    21. };
    To copy to clipboard, switch view to plain text mode 


    2.Subclass QwtPlotMarker
    Qt Code:
    1. class Marker:public QwtPlotMarker
    2. {
    3. public:
    4. Marker():QwtPlotMarker(){}
    5. void drawAt( QPainter *, const QRectF &, const QPointF & ) const
    6. {
    7. symbol().drawSymbol(painter, pos);
    8. QwtPlotMarker::drawAt(painter, rect, pos);
    9. }
    10. };
    To copy to clipboard, switch view to plain text mode 


    3. instantiate and use
    ........
    Qt Code:
    1. Marker *mrk = new Marker();
    2. mrk->setLinePen(QPen(Qt::black, 0, Qt::DashDotLine));
    3. mrk->setLineStyle(QwtPlotMarker::HLine);
    4. mrk->setSymbol(new Symbol(QColor(Qt::yellow), QColor(Qt::green), QSize(15,15)));
    5. mrk->setValue(10.0,10.0);
    To copy to clipboard, switch view to plain text mode 
    ............

    Problem I face:while the marker is displayed fine, symbol is not drawn...

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

    Default Re: Custom symbol for QwtPlotMarker

    You forgot to implement Symbol::clone().

    Uwe

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

    Default Re: Custom symbol for QwtPlotMarker

    sorry for being such a newbie... but qwt_symbol.h doesn't have any clone()
    What and how should i implement clone()...
    Thanks for your time, Uwe

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

    Default Re: Custom symbol for QwtPlotMarker

    Ah sorry you are on Qwt 6 - the right answer here is: you can overload virtual methods only.

    This one is what you are looking for:

    Qt Code:
    1. virtual void drawSymbols( QPainter *,
    2. const QPointF *, int numPoints ) const;
    To copy to clipboard, switch view to plain text mode 
    Uwe

  5. The following 2 users say thank you to Uwe for this useful post:

    pkj (23rd February 2011), sonulohani (29th May 2012)

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

    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 

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

    sonulohani (24th May 2012)

  8. #6
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Angry Re: Custom symbol for QwtPlotMarker

    It is showing error------->

    error: 'drawAt' is not a member of 'QwtPlotMarker'

  9. #7
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Custom symbol for QwtPlotMarker

    There isn't QwtPlotMarker::drawAt() there's Marker::drawAt().
    If you're using pointers, you have to cast QwtPlotMarker to Marker to be able to use methods added in derived class.

  10. The following user says thank you to Spitfire for this useful post:

    sonulohani (28th May 2012)

  11. #8
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Question Re: Custom symbol for QwtPlotMarker

    Sir, could you please give a well defined example on drawSymbols() as i want to draw a symbol based on image .

  12. #9
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Custom symbol for QwtPlotMarker

    this example is wrong since it is calling a virtual method i.e. drawSymbols().

  13. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Custom symbol for QwtPlotMarker

    ... and what is wrong with calling a virtual function? Nothing, and it has nothing to do with the error message you are getting either I guess.

    How about you actually try to participate in solving your problem in future. Just saying "It doesn't work", or asking over and over and over for someone to give you a canned solution you can copy without thought, is often unproductive and unlikely to win you friends. Tells us what errors you are seeing, what you have tried to fix it etc. Ask a smart question.

    Just to see the end of this, a complete example with a custom pixmap as the symbol for both a marker and the data points:
    Qt Code:
    1. #include <QApplication>
    2. #include <QPainter>
    3.  
    4. #include <qwt_plot_curve.h>
    5. #include <qwt_plot.h>
    6. #include <qwt_plot_marker.h>
    7. #include <qwt_symbol.h>
    8.  
    9. class Symbol:public QwtSymbol
    10. {
    11. public:
    12. Symbol(const QPixmap &pixmap);
    13. QSize boundingSize() const;
    14. void drawSymbols( QPainter *, const QPointF *, int numPoints ) const;
    15. private:
    16. QPixmap m_pixmap;
    17. };
    18.  
    19. Symbol::Symbol(const QPixmap &pixmap):
    20. QwtSymbol(QwtSymbol::UserStyle),
    21. m_pixmap(pixmap)
    22. {
    23. }
    24.  
    25. QSize Symbol::boundingSize() const
    26. {
    27. return m_pixmap.size();
    28. }
    29.  
    30.  
    31. void Symbol::drawSymbols(QPainter *painter, const QPointF *points, int numPoints) const
    32. {
    33. painter->save();
    34. for(int i = 0; i < numPoints; ++i)
    35. painter->drawPixmap(points[i], m_pixmap);
    36. painter->restore();
    37. }
    38.  
    39. int main (int argc, char **argv)
    40. {
    41. QApplication a(argc, argv);
    42.  
    43. const int Size = 30;
    44. double xval[Size];
    45. double yval[Size];
    46. for(int i = 0; i < Size; ++i) {
    47. xval[i] = double(i) * 10.0 / double(Size - 1);
    48. yval[i] = qSin(xval[i]) * qCos(2.0 * xval[i]);
    49. }
    50.  
    51. QwtPlot myPlot;
    52. QwtPlotCurve curve("Curve");
    53. curve.setRawSamples(xval, yval, Size);
    54. QPixmap p1("p1.png");
    55. curve.setSymbol(new Symbol(p1));
    56. curve.attach(&myPlot);
    57.  
    58. mrk.setLinePen(QPen(Qt::black, 0, Qt::DashDotLine));
    59. mrk.setValue(0.5, 0.5);
    60. mrk.setLineStyle(QwtPlotMarker::HLine);
    61. QPixmap p2("p2.png");
    62. mrk.setSymbol(new Symbol(p2));
    63. mrk.attach(&myPlot);
    64.  
    65. myPlot.resize(640,480);
    66. myPlot.show();
    67.  
    68. return a.exec();
    69. }
    To copy to clipboard, switch view to plain text mode 
    Built and tested Qwt 6.0.1 on Linux. Make your own p1.png and p2.png markers.
    Last edited by ChrisW67; 30th May 2012 at 09:05.

  14. The following user says thank you to ChrisW67 for this useful post:

    sonulohani (1st June 2012)

  15. #11
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Question Re: Custom symbol for QwtPlotMarker

    Thank you ChrisW67, you're awesome.


    Added after 1 34 minutes:


    Sir,
    I want to ask one last thing i.e.; whenever i am trying to write class code i.e.; symbol, in header file then it is giving error as first defined here pointing to constructor. Why is this happening????
    Last edited by sonulohani; 1st June 2012 at 08:48.

  16. #12
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Cool Re: Custom symbol for QwtPlotMarker

    I understood what is the problem. A very very thanks to you. Its working now. And sorry for querying again and again. I sometimes become insane. But atlast by your help, i figured out what was my mistake. May God bless you.

  17. #13
    Join Date
    Jun 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Custom symbol for QwtPlotMarker

    Hi,

    I copied ChrisW67's example into my project and modified it just a little bit (renamed the Symbol class, made it use just a predefined static file and just added the marker). Now I have got the problem that there is no marker displayed. Even when I use a Symbol shipped with Qwt it is not displayed. Does anybody know what I might be doing wrong?

    Any help is appreciated.
    Thanks in advance.

    David


    Added after 5 minutes:


    Sorry for bothering you guys.
    I solved it myself.
    Really stupid mistake. The image file was in the wrong place.
    Last edited by SeniorSpielbergo; 3rd June 2012 at 16:11.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.