QCustomPlot - show item on QCPAxisRect below customPlot

假如想象 提交于 2019-12-12 08:23:01

问题


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


回答1:


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) );


来源:https://stackoverflow.com/questions/29444939/qcustomplot-show-item-on-qcpaxisrect-below-customplot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!