In a project resembling the QCustomPlot financial demo I want to draw a QCPItemRect not only into the chart area, but also to the area below the chart.
Having
QCPAxisRect * xRect = new QCPAxisRect( this->ui.customPlot )
...
this->ui.customPlot->plotLayout()->addElement(1, 0, xRect);
I want to add the QCPItemRect like
QCPItemRect * xItem = new QCPItemRect( this->ui.customPlot );
xItem -> setPen ( QPen ( Qt::black ));
xItem -> bottomRight ->setAxisRect( this->xRect );
xItem -> topLeft ->setAxisRect( this->xRect );
xItem -> bottomRight ->setCoords(x - 2.0, y - 2.0);
xItem -> topLeft ->setCoords(x + 2.0, y + 2.0);
this->ui.customPlot->addItem( xItem );
However, the rectangle still gets drawn onto this->ui.customPlot
as opposed to this->xRect
. Why?
Any help is much appreciated, Daniel
UPDATE Found a part of the answer myself, one missing line of code is
xItem -> setClipAxisRect( xRect )
Still works only with some QCPAxisRects.
UPDATE 2 Still not there. The following is the smallest code snippet that reproduces the behavior - its enough to paste it into an empty QCustomPlot project:
// create a rectAxis, put it below the main plot
QCPAxisRect * xRect = new QCPAxisRect( this->ui.customPlot );
this->ui.customPlot->plotLayout()->addElement( 1, 0, xRect );
// create a rectItem and show it on the xRect
QCPItemRect * xRectItem = new QCPItemRect( this->ui.customPlot );
xRectItem->setVisible (true);
xRectItem->setPen (QPen(Qt::transparent));
xRectItem->setBrush (QBrush(Qt::lightGray));
xRectItem->topLeft ->setType(QCPItemPosition::ptPlotCoords);
xRectItem->topLeft ->setAxisRect( xRect );
xRectItem->topLeft ->setCoords( 1, 4 );
xRectItem->bottomRight ->setType(QCPItemPosition::ptPlotCoords);
xRectItem->bottomRight ->setAxisRect( xRect );
xRectItem->bottomRight ->setCoords( 2, 1 );
xRectItem->setClipAxisRect ( xRect );
xRectItem->setClipToAxisRect ( false ); // XXX
this->ui.customPlot->replot();[/code]
The behavior depends on whether the "XXX" line is commented out or not
Any hint is much appreciated, Daniel
Found the answer (thanks to the author of QCustomPlot). The missing components were
Specifically,
xRectItem->setClipAxisRect ( xRect );
and
xRectItem->topLeft ->setAxes( xRect->axis(QCPAxis::atBottom), xRect->axis(QCPAxis::atLeft) );
xRectItem->bottomRight ->setAxes( xRect->axis(QCPAxis::atBottom), xRect->axis(QCPAxis::atLeft) );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With