QCustomPlot-在customPlot下方的QCPAxisRect上显示项目
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->xRect
相反,矩形仍然绘制在this->ui.customPlot
上。为什么?
非常感谢您的帮助,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"行是否被注释掉
- 行注释掉了-矩形根本不会出现
- 中的左线-矩形被绘制到主矩形中,如图所示
非常感谢任何提示,Daniel
找到了答案(感谢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) );
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 字符串-C++后显示的随机字符
- 继承期间显示未知行为的子类
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- 程序崩溃并显示"std::out_of_range"错误
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- 从数据库实时显示QT c++中的数据
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 显示错误输出的简单数组排序程序
- Qt自定义QPush按钮未显示在布局上
- C++射线示踪剂ppm表示没有足够的数据来显示图像
- 显示基于用户输入的整数的字符
- 使用QTreeView,如何通过调用函数只突出显示特定的行/列
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- 如何使 windows 命令提示符在C++可执行文件上显示返回值?
- 密码登录程序将永远循环并显示不正确的结果
- QCustomPlot-在customPlot下方的QCPAxisRect上显示项目