Results 1 to 10 of 10

Thread: How to fill color between two curves

  1. #1
    Join Date
    Sep 2014
    Location
    San Francisco
    Posts
    11
    Thanks
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default How to fill color between two curves

    I need to fill color between curves. For example, in the attachment, the grids are created by QwtPlotCurve, I need to fill color like that in all cells. Does anyone have example codes?

    Thanks!
    Attached Images Attached Images

  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: How to fill color between two curves

    The screen shot looks like displaying raster data, but where are those curves you are referring to and between which curves you want to fill something ?

    Uwe

  3. #3
    Join Date
    Sep 2014
    Location
    San Francisco
    Posts
    11
    Thanks
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to fill color between two curves

    Hi Uwe,

    Thank you for your reply!

    The "curves" are the grids. Instead of using "QwtPlotGrid", I draw the horizontal lines and vertical lines by using QwtPlotCurve. My purpose is fill color into any small cell(rectangle) between four lines as long as I have the coordinates of a point. For example, assume I have a point (1,1) and it locates inside the cell in the lower-left corner, by using the coordinates of this point, I need to fill the lower-left cell in the graph with some color. The color not important for now, it is based on another value. However, the most important is how to fill a cell. I am able to use the coordinates of a point to get the right cell in QRectF type. I thought "QwtPlotIntervalCurve::drawSymbols" or " QwtPlotIntervalCurve::drawTube" might be helpful, if so, how can I get the value of "const QwtScaleMap" parameter?

  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: How to fill color between two curves

    When using one of the ready-made plot items I would recommend QwtPlotShape items - one for each cell. Each shape item ( a rectangle is just a special painter path ) can have a pen and a fill brush. This would be very easy to implement, but of course not the most performant way, when having thousands of cells.

    But you could also consider to implement your own type of plot item ( I would do this if I were in your shoes ). In the end all that needs to be done is to translate the plot coordinates of each cell to widget coordinates ( see QwtScaleMap ) and do a QPainter::drawRect() then. Drawing the cells with such an item could be implemented more efficient as you can iterate in steps over the visible area instead of having to iterate over all cells sorting those out, that are outside.

    Uwe

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

    theofthe (30th September 2014)

  6. #5
    Join Date
    Sep 2014
    Location
    San Francisco
    Posts
    11
    Thanks
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to fill color between two curves

    Hi Uwe,

    Thanks for your suggestions, which are very valuable and I am trying both of them now.

    Just one thing, when I use QwtPlotCurve to draw curves (grid in my case), I don't need to override paintEvent function, the curves can be drawn properly. However, when I draw rectangle cell by using QwtPlotShapeItem, I need to override the paintEvent for attaching the object of QwtPlotShapeItem. Otherwise, the rectangle cell can't be drawn.

    May I ask why?

    If you need to code, please let me know.

    Thanks.


    Added after 1 5 minutes:


    BTW, I am using Qt 5.2.1 and Qwt 6.1.1.
    Last edited by theofthe; 29th September 2014 at 21:56.

  7. #6
    Join Date
    Sep 2014
    Location
    San Francisco
    Posts
    11
    Thanks
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to fill color between two curves

    There is another problem, once I attach the second cell (object of QwtPlotShapeItem) on plot, the first cell item will be detached from the plot, which causes there is only one cell displayed. Is there any way to avoid to detach any previous items while attaching the new one?

  8. #7
    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: How to fill color between two curves

    a) plot items are no widgets and don't have any paint events that can be overloaded. Also drawing a rectangle with a shape item is a common use case, where you don't need to do more, than setting the rectangle ( in plot coordinates ) + pen + brush.

    b) nothing is detached, when attaching a new plot item

    For b) I can imagine, that you are trying to use the same item for all grids. Then - when using QwtPlotShapeItem::setRect() - you would indeed erase the previous painter path by setting it to a new one. In general a QPainterPath could consist of many rectangles, ( using QPainterPath::addRect() ) but it can be painted with one brush only and you can't fill the different cells with different colors this way.

    Maybe have a look at the itemeditor example that is using different shape items - one of them is a rectangle.

    Uwe

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

    theofthe (30th September 2014)

  10. #8
    Join Date
    Sep 2014
    Location
    San Francisco
    Posts
    11
    Thanks
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to fill color between two curves

    Thanks Uwe! Your imagination is correct. The problem is solved now.

  11. #9
    Join Date
    Sep 2014
    Location
    San Francisco
    Posts
    11
    Thanks
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to fill color between two curves

    Btw, about itemeditor project, in file plot.cpp, inside the constructor of Plot, an object of QwtPlotGrid is allocated but not released, which causes a memory leak.

  12. #10
    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: How to fill color between two curves

    Plot items are released together with the plot, where they are attached to - like a QObject gets deleted, when its parent is deleted.

    Uwe

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

    theofthe (2nd October 2014)

Similar Threads

  1. Replies: 5
    Last Post: 17th December 2014, 12:07
  2. Some problem while fill color in QPainterPath
    By ymc123405 in forum Qt Programming
    Replies: 2
    Last Post: 29th August 2014, 17:48
  3. Replies: 0
    Last Post: 27th January 2014, 18:26
  4. Replies: 1
    Last Post: 9th May 2011, 15:19
  5. How to fill the grid with the color?
    By merry in forum Qt Programming
    Replies: 3
    Last Post: 18th June 2007, 11:10

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.