Search code examples
c++qtqwt

Set interval for X and Y QwtPlotZoneItem


I need to plot a region on a graph using the QwtPlotZoneItem class. I need to set different intervals for both X and Y axes, how can I do that?

The orientation of my QwtPlotZoneItem is vertical, so based on the documentation, if I set the interval it will by applied to the X axis only.

"For a horizontal zone the interval is related to the y axis, for a vertical zone it is related to the x axis."

My constructor sets:

setOrientation( Qt::Vertical );
setInterval( initDate, endDate );

Basically, what I need is to create multiple rectangles on my graph that represents the regions, for example:

enter image description here

Qt 5.3.2

Qwt 6.1.0


Solution

  • I was trying to use an incorrect class for my purposes. I found a note in the documentation:

    "For displaying an area that is bounded for x and y coordinates use QwtPlotShapeItem"

    The QwtPlotShapeItem class does exactly what I need.

    I need basically to set the brush and create the rectangle, for example:

    QwtPlotShapeItem *shapeItem = new QwtPlotShapeItem();
    
    shapeItem->setBrush(QColor(255,255,255, 0));
    shapeItem->setPen(QColor(Qt::transparent), 0.0, Qt::SolidLine);
    
    // TopLeft - BottomRight
    QRect myRect(QPoint(startDate, yUpperPos), QPoint(endDate, yLowerPos));
    shapeItem->setRect( QRectF(myRect) );
    
    shapeItem->attach(myQwtPlot);
    myQwtPlot->replot();