Results 1 to 5 of 5

Thread: Custom CurveStyle for qwt_plot_curve

  1. #1
    Join Date
    Apr 2008
    Posts
    53
    Thanks
    10

    Default Custom CurveStyle for qwt_plot_curve

    Hi,

    I needed to inherit from qwt_plot_curve to add another CurveStyle (DottedLines) to the enum:

    Qt Code:
    1. /*
    2.   - UserCurve
    3.   Styles >= UserCurve are reserved for derived
    4.   classes of QwtPlotCurve that overload drawCurve() with
    5.   additional application specific curve types.
    6. */
    7. enum CurveStyle
    8. {
    9. NoCurve,
    10.  
    11. Lines,
    12. Sticks,
    13. Steps,
    14. Dots,
    15.  
    16. UserCurve = 100
    17. };
    To copy to clipboard, switch view to plain text mode 

    I wanted to make a style called DottedLine, which connects every other point with a line. I need to derive a class and then redefine DrawCurve:

    Qt Code:
    1. void QwtPlotCurve::drawCurve(QPainter *painter, int style,
    2. const QwtScaleMap &xMap, const QwtScaleMap &yMap,
    3. int from, int to) const
    4. {
    5. switch (style)
    6. {
    7. case Lines:
    8. if ( testCurveAttribute(Fitted) )
    9. {
    10. // we always need the complete
    11. // curve for fitting
    12. from = 0;
    13. to = dataSize() - 1;
    14. }
    15. drawLines(painter, xMap, yMap, from, to);
    16. break;
    17. case Sticks:
    18. drawSticks(painter, xMap, yMap, from, to);
    19. break;
    20. case Steps:
    21. drawSteps(painter, xMap, yMap, from, to);
    22. break;
    23. case Dots:
    24. drawDots(painter, xMap, yMap, from, to);
    25. break;
    26. case DottedLines:
    27. drawDottedLines(painter, xMap, yMap, from, to);
    28. break;
    29. case NoCurve:
    30. default:
    31. break;
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 

    (I added drawDottedLines at the bottom). How can I inherit the enum CurveStyles such that I can set the style using:

    void QwtPlotCurve::setStyle(CurveStyle style)?

    I can't inherit the enum right? Do I need to redefine setStyle to:

    void MyPlotCurve::setStyle(MyCurveStyle style)? And if so, how do I define MyCurveStyle?

    I hope this is not too confusing, thanks

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

    Default Re: Custom CurveStyle for qwt_plot_curve

    Qt Code:
    1. class YourCurve: public QwtPlotCurve
    2. {
    3. enum YourStyle
    4. {
    5. DottedLines = QwtPlotCurve::UserStyle + 1000
    6. }
    7.  
    8. ....
    9.  
    10. virtual void drawCurve(QPainter *painter, int style,
    11. const QwtScaleMap &xMap, const QwtScaleMap &yMap,
    12. int from, int to) const
    13. {
    14. if ( style < QwtPlotCurve::UserStyle )
    15. QwtPlotCurve::drawCurve(painter, style, xMap, yMap, from, to);
    16. else
    17. drawYourCurve(painter, style, xMap, yMap, from, to);
    18. }
    19. };
    To copy to clipboard, switch view to plain text mode 

    HTH,
    Uwe

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

    jmsbc (15th April 2009)

  4. #3
    Join Date
    Apr 2008
    Posts
    53
    Thanks
    10

    Default Re: Custom CurveStyle for qwt_plot_curve

    Thanks Uwe!

    Now that I got that problem solved, I'm trying to connect every other point in the QwtPlotCurve. I added a function for drawing DottedLines, which looks like this:

    Qt Code:
    1. void MyQwtPlotCurve::drawDottedLines(QPainter *painter,
    2. const QwtScaleMap &xMap, const QwtScaleMap &yMap,
    3. int from, int to) const
    4. {
    5. if (!connectLine)
    6. {
    7. prevX = xMap.transform(to);
    8. prevY = yMap.transform(to);
    9. connectLine = true;
    10. } else
    11. {
    12. const int xi = xMap.transform(x(to));
    13. const int yi = yMap.transform(y(to));
    14.  
    15. QwtPainter::drawLine(painter, prevX, prevY, xi, yi);
    16. connectLine = false;
    17. }
    To copy to clipboard, switch view to plain text mode 

    The bool connectLine is just a counter to keep track of drawing for every other point. Also, the parameters "from" and "to" are always equal to each other. I was trying to implement something like DrawSticks, but this isn't quite working correctly. A line keeps getting drawn from the last point to some crazy point. What am I doing wrong, please help!

    Thanks a lot again

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

    Default Re: Custom CurveStyle for qwt_plot_curve

    Qt Code:
    1. prevX = xMap.transform(to);
    2. prevY = yMap.transform(to);
    To copy to clipboard, switch view to plain text mode 

    Mapping an index - did you look at your code yourself ?

    But if "from" is always the same as "to" you have degenerated curves consisting of one point ( what is a marker ). Don't do this.

    Uwe

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

    jmsbc (15th April 2009)

  7. #5
    Join Date
    Apr 2008
    Posts
    53
    Thanks
    10

    Default Re: Custom CurveStyle for qwt_plot_curve

    Quote Originally Posted by Uwe View Post
    Qt Code:
    1. prevX = xMap.transform(to);
    2. prevY = yMap.transform(to);
    To copy to clipboard, switch view to plain text mode 

    Mapping an index - did you look at your code yourself ?

    But if "from" is always the same as "to" you have degenerated curves consisting of one point ( what is a marker ). Don't do this.

    Uwe
    Sorry about that, I was testing a bunch of different code in that function, so I didn't catch that mistake.

    After going back to debug the code, I realized that drawDottedLines is not only called when I explicitly call draw(int from, int to), but it is called to repaint the canvas each time the canvas changes from a paintevent. So then I realized every point or line in the curve need to be re-drawn each time drawDottedLines(...) gets called. (When I was debugging before, for some reason I saw that "from" and "to" were always the same number so I thought one point was being painted at a time)

    Here's the code which works now:
    Qt Code:
    1. void MyQwtPlotCurve::drawDottedLines(QPainter *painter,
    2. const QwtScaleMap &xMap, const QwtScaleMap &yMap,
    3. int from, int to) const
    4. {
    5. for (int i = from; i <= to; i++)
    6. {
    7. if (((i+1) % 2) == 0)
    8. {
    9. const int prevX = xMap.transform(x(i - 1));
    10. const int prevY = yMap.transform(y(i - 1));
    11. const int xi = xMap.transform(x(i));
    12. const int yi = yMap.transform(y(i));
    13.  
    14. QwtPainter::drawLine(painter, prevX, prevY, xi, yi);
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    Thanks again Uwe

Similar Threads

  1. Phonon + custom media source
    By wysota in forum What's New in Qt 4.4
    Replies: 2
    Last Post: 18th July 2008, 17:41
  2. Replies: 2
    Last Post: 16th May 2008, 14:39
  3. Custom widget
    By zorro68 in forum Qt Programming
    Replies: 7
    Last Post: 28th January 2008, 14:06
  4. Custom proxy model issue
    By Khal Drogo in forum Qt Programming
    Replies: 13
    Last Post: 30th November 2007, 12:41
  5. custom plug-in widget in another custom plug-in widget.
    By MrGarbage in forum Qt Programming
    Replies: 6
    Last Post: 27th August 2007, 15:38

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.