Results 1 to 7 of 7

Thread: QwtCurve with multiple colors

  1. #1
    Join Date
    Dec 2013
    Location
    Jerada, Morroco
    Posts
    106
    Thanks
    11
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QwtCurve with multiple colors

    Hi everybody,

    I want to know if there is a possibility to draw one qwtCurve with different colors ? if yes how ?

    Thank you.

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

    Default Re: QwtCurve with multiple colors

    You have to overload one of the draw methods of QwtPlotCurve. For instance if your curve is a line plot and you want to have segments with different line pens:

    Qt Code:
    1. YourCurve::drawLines(QPainter *painter, const QwtScaleMap &xMap, const QwtScaleMap &yMap,
    2. const QRectF &canvasRect, int from, int to ) const
    3. {
    4. for ( int i = 0; i < numSegments; i++ )
    5. {
    6. ....
    7.  
    8. painter->setPen( segmentPen[i] );
    9. QwtPlotCurve::drawLines(painter ,xMap, yMap, canvasRect,
    10. segmentStart[i], segmentStart[i+1] );
    11. }
    To copy to clipboard, switch view to plain text mode 
    When you are not using QwtPlotDirectPainter from/to should always be for the complete curve.

    Uwe

  3. #3
    Join Date
    Dec 2013
    Location
    Jerada, Morroco
    Posts
    106
    Thanks
    11
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QwtCurve with multiple colors

    Can you give me please un exemple how i can call the method drawLines? i don't know what is QwtScaleMap ? also what do you want to say by segmentPen[]

    Thank you in advance

  4. #4
    Join Date
    Dec 2013
    Location
    Toronto, Canada
    Posts
    62
    Thanked 16 Times in 15 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QwtCurve with multiple colors

    i don't know what is QwtScaleMap
    The doc says..
    QwtScaleMap offers transformations from the coordinate system of a scale into the linear coordinate system of a paint device and vice versa.
    also what do you want to say by segmentPen[]
    The colors effectively divide the curve into segments ...
    Thus, if you have 3 colors in the curve, you have 3 segments

    Thus, numSegments == 3
    int segmentPen[numSegments] ... initialize the array with the pen you want

    I believe thats it

  5. #5
    Join Date
    Dec 2013
    Location
    Jerada, Morroco
    Posts
    106
    Thanks
    11
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QwtCurve with multiple colors

    hi,

    This my code ;
    class CCurvePlot :
    public QwtPlotCurve
    {
    public:
    CCurvePlot(const QwtText &title);
    CCurvePlot(const QString &title = QString::null );
    ~CCurvePlot(void);
    protected:
    virtual void drawLines( QPainter *painter, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, int from, int to ) const;
    };


    void CCurvePlot::drawLines( QPainter *painter, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, int from, int to ) const
    {
    for ( int i = 1; i < 20; i++ )
    {
    if(0 < i && i < 5 )
    painter->setPen(Qt::red );
    if(5 < i && i < 10 )
    painter->setPen(Qt::blue );
    if(10 < i && i < 15 )
    painter->setPen(Qt::green );
    if(15 < i && i < 20 )
    painter->setPen(Qt::yellow );

    drawLines(painter ,xMap, yMap, canvasRect, i *10, (i+1) *10 );
    }
    }

    i call this in other classe

    CCurvePlot* pCurve = new CCurvePlot();
    pCurve->setSamples( samples );// sample is a vector of QPointF

    But it doesn't work.

  6. #6
    Join Date
    Dec 2013
    Location
    Toronto, Canada
    Posts
    62
    Thanked 16 Times in 15 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QwtCurve with multiple colors

    Qt Code:
    1. #ifndef CUSTOMCURVE_H
    2. #define CUSTOMCURVE_H
    3.  
    4. #include <qwt_plot_curve.h>
    5.  
    6. /*
    7.  *QVector<QPointF> samplesVec()
    8. {
    9.   QVector<QPointF> result;
    10.   for(int i=0; i<=300; ++i)
    11.   result << QPointF(i, 8+i*i); //y=8+x^2
    12.   return result;
    13. }
    14. */
    15.  
    16. class CustomCurve : public QwtPlotCurve
    17. {
    18. public:
    19. explicit CustomCurve(const QString &title=QString::null):QwtPlotCurve (title)
    20. {
    21.  
    22. }
    23.  
    24. virtual void drawLines (QPainter *p, const QwtScaleMap &xMap,
    25. const QwtScaleMap &yMap, const QRectF &canvasRect, int from, int to) const
    26. {
    27.  
    28. const int numOfSegments = m_segColor.size();
    29. if(numOfSegments)
    30. {
    31. p->save();
    32. for(int i=0; i<numOfSegments; ++i)
    33. {
    34. p->setPen(m_segColor[i]);
    35. QwtPlotCurve::drawLines (p, xMap, yMap, canvasRect, m_segStart[i], m_segFinish[i]);
    36. }
    37. p->restore();
    38. }
    39. else
    40. QwtPlotCurve::drawLines (p, xMap, yMap, canvasRect, from, to);
    41.  
    42. }
    43.  
    44. void setSegmentInfo(int segmentStartIndex, int segmentFinisIndex, const QColor & color)
    45. {
    46. m_segColor.push_back(color);
    47. m_segStart.push_back(segmentStartIndex);
    48. m_segFinish.push_back(segmentFinisIndex);
    49.  
    50. }
    51.  
    52. private:
    53. QList<QColor> m_segColor;
    54. QList<int> m_segStart;
    55. QList<int> m_segFinish;
    56.  
    57.  
    58.  
    59. };
    60.  
    61. #endif // CUSTOMCURVE_H
    To copy to clipboard, switch view to plain text mode 


    If you have 300 points in a samples vec..

    Qt Code:
    1. ....
    2.  
    3. curve->setSegmentInfo(0, 100, QColor("red"));
    4. curve->setSegmentInfo(100, 200, QColor("green"));
    5. curve->setSegmentInfo(200, 300, QColor("blue"));
    6. ...
    To copy to clipboard, switch view to plain text mode 

    Then the above calls to setSegmentInfo() by a client of a CustomCurve instance
    will draw the curve in three colors..

  7. #7
    Join Date
    Dec 2013
    Location
    Jerada, Morroco
    Posts
    106
    Thanks
    11
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QwtCurve with multiple colors

    Thank you it work now

Similar Threads

  1. QLabel - text in multiple colors
    By galhajaj in forum Qt Programming
    Replies: 1
    Last Post: 15th June 2012, 15:09
  2. Replies: 1
    Last Post: 18th July 2011, 07:56
  3. qwtcurve vector
    By 21did21 in forum Qwt
    Replies: 4
    Last Post: 18th June 2011, 00:07
  4. Replies: 1
    Last Post: 7th April 2010, 16:26
  5. Replies: 1
    Last Post: 13th January 2010, 17:21

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.