QCustomPlot - 在customPlot下面的QCPAxisRect上显示项目

Dan*_*cik 12 c++ qt qcustomplot

在类似于QCustomPlot财务演示的项目中,我想将QCPItemRect不仅绘制到图表区域,还绘制到图表下方的区域.

QCPAxisRect *   xRect = new QCPAxisRect( this->ui.customPlot )
...
this->ui.customPlot->plotLayout()->addElement(1, 0, xRect);          
Run Code Online (Sandbox Code Playgroud)

我想添加QCPItemRect之类的

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 );
Run Code Online (Sandbox Code Playgroud)

然而,矩形仍然被绘制this->ui.customPlot而不是this->xRect.为什么?

Daniel,非常感谢任何帮助

更新 我自己找到了答案的一部分,缺少一行代码

xItem -> setClipAxisRect( xRect )
Run Code Online (Sandbox Code Playgroud)

仍然只适用于某些QCPAxisRects.

更新2 仍然没有.以下是重现行为的最小代码片段 - 足以将其粘贴到空的QCustomPlot项目中:

// 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]
Run Code Online (Sandbox Code Playgroud)

行为取决于"XXX"行是否被注释掉

  1. line注释掉 - 矩形不会出现在所有位置.
  2. 线留在-矩形被抽进主RECT,如所示这里.

丹尼尔非常感谢任何提示

Dan*_*cik 6

找到了答案(感谢QCustomPlot的作者).缺少的组件是

  1. 设置矩形的clipAxisRect(已包含在上次更新的问题中)
  2. 设置矩形服从的轴.

特别,

 xRectItem->setClipAxisRect     ( xRect );
Run Code Online (Sandbox Code Playgroud)

 xRectItem->topLeft     ->setAxes( xRect->axis(QCPAxis::atBottom), xRect->axis(QCPAxis::atLeft) );
 xRectItem->bottomRight ->setAxes( xRect->axis(QCPAxis::atBottom), xRect->axis(QCPAxis::atLeft) );
Run Code Online (Sandbox Code Playgroud)