QCustomPlot - show item on QCPAxisRect below customPlot
Asked Answered
E

1

12

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

  1. line commented out - the rectangle does not appear AT ALL.
  2. line left in - the rectangle gets drawn into the main rect, such as shown here.

Any hint is much appreciated, Daniel

Enmesh answered 4/4, 2015 at 9:46 Comment(0)
E
6

Found the answer (thanks to the author of QCustomPlot). The missing components were

  1. Settings the clipAxisRect of the rectangle (already contained in the last update of question)
  2. Settings the axes, which the rectangle obeys.

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) );
Enmesh answered 6/5, 2015 at 7:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.