[英]QCustomPlot - show item on QCPAxisRect below customPlot
在類似QCustomPlot財務演示的項目中,我想將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
。 為什么?
Daniel,非常感謝任何幫助
更新我自己找到了答案的一部分,缺少一行代碼
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”行是否被注釋掉
丹尼爾非常感謝任何提示
找到了答案(感謝QCustomPlot的作者)。 缺少的組件是
特別,
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) );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.