在一个类似的项目中QCustomPlot 财务演示 http://qcustomplot.com/index.php/demos/financialchartsdemo我不仅想在图表区域中绘制一个 QCPItemRect,还想在图表下方的区域中绘制一个 QCPItemRect。
Having
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
. Why?
任何帮助深表感谢,
丹尼尔
UPDATE我自己找到了答案的一部分,缺少一行代码是
xItem -> setClipAxisRect( xRect )
仍然仅适用于某些 QCPAxisRect。
UPDATE 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 https://www.dropbox.com/s/rp8ouphbvfcjxyn/qcustomplot_axisRect.GIF?dl=0.
非常感谢任何提示,
丹尼尔