Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QCustomPlot - show item on QCPAxisRect below customPlot

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

like image 778
Daniel Bencik Avatar asked Apr 04 '15 09:04

Daniel Bencik


1 Answers

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) );
like image 70
Daniel Bencik Avatar answered Sep 28 '22 09:09

Daniel Bencik