• Qwt QwtPlotMarker标记类详解


    1.概述

    QwtPlotMarker类是Qwt绘图库中用于在图表上绘制标记的类。标记可以是垂直或水平线、直线、文本或箭头等。它可用于标记某个特定的位置、绘制参考线或注释信息。

    以下是类继承关系图:

    2.常用方法

    设置标记的坐标。传入x和y坐标值,标记将被放置在指定的位置。

    setValue(double x, double y)

    分别设置标记的x和y坐标。可以单独调用这些方法来更改标记的位置。

    setXValue(double x)和setYValue(double y)

    设置标记的文本标签。

    1. void setLabel (const QwtText &)

    设置标记的线条样式。可以选择实线、虚线和点线等样式。

    setLineStyle(QwtPlotMarker::LineStyle style)

    设置标记的线条画笔。传入一个QPen类型的对象,用于设定线条的颜色、宽度等属性。

    setLinePen(const QPen &pen)

    设置标记的符号样式。传入一个QwtSymbol类型的对象,用于设定标记的符号样式。

    setSymbol(QwtSymbol *symbol)

    将标记附加到或从QwtPlot中分离。attach()方法用于将标记添加到QwtPlot图表中,而detach()方法则用于从图表中移除标记。

    attach(QwtPlot *plot)和detach()

    3.代码示例

    画了两根曲线,sin和cos,中间绘制了两个标记QwtPlotMarker,一个代表0轴,一个代表(1,0)点。

    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "qwt_plot.h"
    4. #include "qwt_plot_curve.h"
    5. #include "qwt_text.h"
    6. #include "qwt_legend.h"
    7. #include "qwt_symbol.h"
    8. #include "qwt_plot_marker.h"
    9. MainWindow::MainWindow(QWidget *parent) :
    10. QMainWindow(parent),
    11. ui(new Ui::MainWindow)
    12. {
    13. ui->setupUi(this);
    14. QwtPlot *plot = new QwtPlot(QwtText("sin and cos"),this);
    15. //设置背景色
    16. plot->setCanvasBackground(QBrush(QColor(Qt::white)));
    17. //添加图例
    18. QwtLegend *legend = new QwtLegend();
    19. plot->insertLegend(legend);
    20. //设置坐标轴标题
    21. plot->setAxisTitle(QwtAxis::YLeft,QwtText("Y坐标轴"));
    22. plot->setAxisTitle(QwtAxis::XBottom,QwtText("X坐标轴"));
    23. plot->setAxisScale(QwtAxis::YLeft, -1, 1);//设置左Y轴范围
    24. plot->setAxisScale(QwtAxis::XBottom, 0, 2);//设置左Y轴范围
    25. // add curves1
    26. QwtPlotCurve *curve1 = new QwtPlotCurve( "cos" );
    27. curve1->setRenderHint( QwtPlotItem::RenderAntialiased );
    28. curve1->setPen( Qt::blue );
    29. //curve1->setBrush(QBrush(QColor(Qt::red)));
    30. curve1->setLegendAttribute( QwtPlotCurve::LegendShowLine );
    31. curve1->setYAxis( QwtAxis::YLeft );
    32. curve1->setStyle(QwtPlotCurve::Lines);
    33. curve1->attach( plot );
    34. // add curves2
    35. QwtPlotCurve *curve2 = new QwtPlotCurve( "sin" );
    36. curve2->setRenderHint( QwtPlotItem::RenderAntialiased );
    37. curve2->setPen( Qt::gray );
    38. //curve3->setBrush(QBrush(QColor(Qt::black)));
    39. curve2->setLegendAttribute( QwtPlotCurve::LegendShowLine );
    40. curve2->setYAxis( QwtAxis::YLeft );
    41. curve2->setStyle(QwtPlotCurve::Lines);
    42. curve2->attach( plot );
    43. int numPoints = 100;
    44. QVector points1;
    45. QVector points2;
    46. for (int i = 0; i < numPoints; ++i) {
    47. double x = 2*i / (double)(numPoints - 1); // x范围从0到2
    48. double y = sin(2 * M_PI * x); // 计算sin函数的y值
    49. double y2 = cos(2 * M_PI * x); // 计算sin函数的y值
    50. points1.append(QPointF(x, y));
    51. points2.append(QPointF(x, y2));
    52. }
    53. curve1->setSamples(points2);
    54. curve2->setSamples(points1);
    55. // 创建水平参考线标记
    56. QwtPlotMarker *lineMarker = new QwtPlotMarker();
    57. lineMarker->setLabel(QwtText("zero axis")); // 设置标记的文本标签
    58. lineMarker->setLabelAlignment(Qt::AlignRight | Qt::AlignTop); // 设置标签的对齐方式
    59. lineMarker->setLabelOrientation(Qt::Horizontal); // 设置标签的方向为水平
    60. lineMarker->setLineStyle(QwtPlotMarker::HLine); // 设置标记的线条样式为水平线
    61. lineMarker->setLinePen(Qt::red, 2.0); // 设置标记的线条颜色和宽度
    62. lineMarker->setValue(0.0, 0.0); // 设置标记的坐标位置
    63. // 创建文本标记
    64. QwtPlotMarker *textMarker = new QwtPlotMarker();
    65. textMarker->setLabel(QwtText("(1,0)")); // 设置标记的文本标签
    66. textMarker->setLabelAlignment(Qt::AlignCenter | Qt::AlignTop); // 设置标签的对齐方式
    67. textMarker->setLabelOrientation(Qt::Horizontal); // 设置标签的方向为水平
    68. textMarker->setLineStyle(QwtPlotMarker::NoLine); // 设置标记没有线条
    69. textMarker->setXValue(1); // 设置标记的x坐标
    70. textMarker->setYValue(0); // 设置标记的y坐标
    71. // 将标记附加到QwtPlot中
    72. lineMarker->attach(plot);
    73. textMarker->attach(plot);
    74. // finally, refresh the plot
    75. plot->replot();
    76. ui->verticalLayout->addWidget(plot);
    77. }
    78. MainWindow::~MainWindow()
    79. {
    80. delete ui;
    81. }

    4.相关参考

    Qwt QwtPlotCurve类详解-CSDN博客

  • 相关阅读:
    深入浅出依赖注入及其在抖音直播中的应用
    YOLOv5 PyQt5 | PyQt5环境配置及组件介绍 | 1/3
    【深度学习3】线性回归与逻辑回归
    ES6生成器(Generator)和迭代器(Iterator)
    一款完全开源免费的快速开发框架:基于LayUI+SpringMVC+Spring+Hibernate+Mysql搭建而成
    Vite -静态资源处理 - 普通的图片
    windows环境下PHP7.4多线程设置
    交叉编译BusyBox
    Python 面试复习题整理
    华为Linux系统开发工程师面试
  • 原文地址:https://blog.csdn.net/wzz953200463/article/details/134063442