QCustomPlot - 在 customPlot 下面的 QCPAxisRect 上显示项目
QCustomPlot - show item on QCPAxisRect below customPlot
在类似the QCustomPlot financial demo的项目中,我不仅要在图表区域绘制QCPItemRect,还要在图表下方的区域绘制QCPItemRect。
拥有
QCPAxisRect * xRect = new QCPAxisRect( this->ui.customPlot )
...
this->ui.customPlot->plotLayout()->addElement(1, 0, xRect);
我想像这样添加 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 );
但是,矩形仍然绘制在 this->ui.customPlot
上,而不是 this->xRect
。为什么?
非常感谢任何帮助,
丹尼尔
更新
自己找到了一部分答案,少了一行代码
xItem -> setClipAxisRect( xRect )
仍然只适用于一些 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]
行为取决于 "XXX" 行是否被注释掉
- 行被注释掉 - 矩形根本没有出现。
- line left in - 矩形被绘制到主矩形中,如图所示 here。
非常感谢任何提示,
丹尼尔
找到答案了(感谢QCustomPlot的作者)。缺少的组件是
- 设置矩形的clipAxisRect(已包含在问题的最后更新中)
- 设置矩形遵循的轴。
具体来说,
xRectItem->setClipAxisRect ( xRect );
和
xRectItem->topLeft ->setAxes( xRect->axis(QCPAxis::atBottom), xRect->axis(QCPAxis::atLeft) );
xRectItem->bottomRight ->setAxes( xRect->axis(QCPAxis::atBottom), xRect->axis(QCPAxis::atLeft) );
在类似the QCustomPlot financial demo的项目中,我不仅要在图表区域绘制QCPItemRect,还要在图表下方的区域绘制QCPItemRect。
拥有
QCPAxisRect * xRect = new QCPAxisRect( this->ui.customPlot )
...
this->ui.customPlot->plotLayout()->addElement(1, 0, xRect);
我想像这样添加 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 );
但是,矩形仍然绘制在 this->ui.customPlot
上,而不是 this->xRect
。为什么?
非常感谢任何帮助, 丹尼尔
更新 自己找到了一部分答案,少了一行代码
xItem -> setClipAxisRect( xRect )
仍然只适用于一些 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]
行为取决于 "XXX" 行是否被注释掉
- 行被注释掉 - 矩形根本没有出现。
- line left in - 矩形被绘制到主矩形中,如图所示 here。
非常感谢任何提示, 丹尼尔
找到答案了(感谢QCustomPlot的作者)。缺少的组件是
- 设置矩形的clipAxisRect(已包含在问题的最后更新中)
- 设置矩形遵循的轴。
具体来说,
xRectItem->setClipAxisRect ( xRect );
和
xRectItem->topLeft ->setAxes( xRect->axis(QCPAxis::atBottom), xRect->axis(QCPAxis::atLeft) );
xRectItem->bottomRight ->setAxes( xRect->axis(QCPAxis::atBottom), xRect->axis(QCPAxis::atLeft) );