PDA

View Full Version : How to fill color between two curves



theofthe
26th September 2014, 05:23
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!

Uwe
26th September 2014, 08:30
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

theofthe
26th September 2014, 17:26
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?

Uwe
27th September 2014, 11:21
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

theofthe
29th September 2014, 21:56
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.

theofthe
30th September 2014, 00:45
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?

Uwe
30th September 2014, 07:23
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

theofthe
30th September 2014, 20:03
Thanks Uwe! Your imagination is correct. The problem is solved now.

theofthe
30th September 2014, 23:14
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.

Uwe
1st October 2014, 06:54
Plot items are released together with the plot, where they are attached to - like a QObject gets deleted, when its parent is deleted.

Uwe