Qwt QwtLegend和QwtPlotLegendItem图例类详解

2023-10-28 02:20

本文主要是介绍Qwt QwtLegend和QwtPlotLegendItem图例类详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.概述

QwtLegend类是Qwt绘图库中用于显示图例的类。图例用于标识不同曲线、绘图元素或数据的意义,以便用户能够更好地理解图表中的数据。通过QwtLegend类,可以方便地在图表中添加、删除和设置图例的位置、方向和样式等属性。

QwtPlotLegendItem类是Qwt绘图库中用于在绘图中添加图例项的类。与QwtLegend类不同,QwtPlotLegendItem类是将图例项直接添加到绘图中,而不是作为独立的图例显示。可以将QwtPlotLegendItem对象与绘图对象相关联,以便在绘图中显示图例项。 

2. 常用方法

QwtPlotLegendItem常用方法介绍

设置最大列数

void setMaxColumns (uint)

设置对齐方式

void setAlignmentInCanvas (Qt::Alignment)

设置背景模式

void setBackgroundMode (BackgroundMode)

设置边框圆角

void setBorderRadius (double)

设置字体

void setFont (const QFont &)

设置外边距

void setItemMargin (int)void setMargin (int)

设置距离

void setItemSpacing (int)void setSpacing (int)

3.示例

源码:

//LegendWidget.h
#ifndef LEGENDWIDGET_H
#define LEGENDWIDGET_H#include <QWidget>namespace Ui {
class LegendWidget;
}class QwtLegend;
class QwtPlotLegendItem;class Settings
{public:Settings(){legend.isEnabled = false;legend.position = 0;legendItem.isEnabled = false;legendItem.numColumns = 0;legendItem.alignment = 0;legendItem.backgroundMode = 0;legendItem.size = 12;curve.numCurves = 0;curve.title = "Curve";}struct{bool isEnabled;int position;} legend;struct{bool isEnabled;int numColumns;int alignment;int backgroundMode;int size;} legendItem;struct{int numCurves;QString title;} curve;
};class LegendWidget : public QWidget
{Q_OBJECTpublic:explicit LegendWidget(QWidget *parent = 0);~LegendWidget();private:Settings settings() const;void applySettings( const Settings& );void insertCurve();private slots:void on_cboxLegendEnabled_stateChanged(int arg1);void on_cbxPos_currentIndexChanged(int index);void on_cboxLegendItemEnabled_stateChanged(int arg1);void on_cbxHorizontal_currentIndexChanged(int index);void on_cbxVertical_currentIndexChanged(int index);void on_cbxBackGround_currentIndexChanged(int index);void on_spinBoxSize_valueChanged(int arg1);void on_spinBoxNum_valueChanged(int arg1);void on_leTitle_textEdited(const QString &arg1);private Q_SLOTS:void edited();void on_spinBoxColumns_valueChanged(int arg1);private:QwtLegend* m_externalLegend = nullptr;QwtPlotLegendItem* m_legendItem = nullptr;bool m_isDirty = false;private:Ui::LegendWidget *ui;
};#endif // LEGENDWIDGET_H#include "LegendWidget.h"
#include "ui_LegendWidget.h"
#include "qwt_plot.h"
#include "qwt_plot_curve.h"
#include "qwt_text.h"
#include "qwt_legend.h"
#include "qwt_symbol.h"
#include "qwt_plot_marker.h"
#include "qwt_plot_grid.h"
#include "qwt_scale_div.h"
#include "qwt_plot_canvas.h"
#include "qwt_plot_legenditem.h"
#include "qwt_math.h"
#include "qwt_plot_layout.h"class Curve : public QwtPlotCurve
{public:Curve( int index ):m_index( index ){setRenderHint( QwtPlotItem::RenderAntialiased );initData();}void setCurveTitle( const QString& title ){QString txt("%1 %2");setTitle( QString( "%1 %2" ).arg( title ).arg( m_index ) );}void initData(){QVector< QPointF > points;double y = qwtRand() % 1000;for ( double x = 0.0; x <= 1000.0; x += 100.0 ){double off = qwtRand() % 200 - 100;if ( y + off > 980.0 || y + off < 20.0 )off = -off;y += off;points += QPointF( x, y );}setSamples( points );}private:const int m_index;
};class LegendItem : public QwtPlotLegendItem
{public:LegendItem(){setRenderHint( QwtPlotItem::RenderAntialiased );const QColor c1( Qt::white );setTextPen( c1 );setBorderPen( c1 );QColor c2( Qt::gray );c2.setAlpha( 200 );setBackgroundBrush( c2 );}
};QwtPlot *g_plot = nullptr;LegendWidget::LegendWidget(QWidget *parent) :QWidget(parent),ui(new Ui::LegendWidget)
{ui->setupUi(this);QwtPlotCanvas* canvas = new QwtPlotCanvas();canvas->setFocusIndicator( QwtPlotCanvas::CanvasFocusIndicator );canvas->setFocusPolicy( Qt::StrongFocus );canvas->setPalette( Qt::black );//创建plotg_plot = new QwtPlot(QwtText("图列示例"),this);g_plot->setFooter( "Footer" );g_plot->setAutoReplot( false );g_plot->setCanvas( canvas );//创建一个网格QwtPlotGrid* grid = new QwtPlotGrid;grid->enableXMin( true );grid->setMajorPen( Qt::gray, 0, Qt::DotLine );grid->setMinorPen( Qt::darkGray, 0, Qt::DotLine );grid->attach( g_plot );//设置坐标轴范围g_plot->setAxisScale( QwtAxis::YLeft, 0.0, 1000.0 );g_plot->setAxisScale( QwtAxis::XBottom, 0.0, 1000.0 );ui->hLayout->addWidget(g_plot);//初始化属性Settings settings;settings.legend.isEnabled = true;settings.legend.position = QwtPlot::BottomLegend;settings.legendItem.isEnabled = false;settings.legendItem.numColumns = 1;settings.legendItem.alignment = Qt::AlignRight | Qt::AlignVCenter;settings.legendItem.backgroundMode = 0;settings.legendItem.size = g_plot->canvas()->font().pointSize();settings.curve.numCurves = 4;settings.curve.title = "曲线";applySettings(settings);
}LegendWidget::~LegendWidget()
{delete ui;
}Settings LegendWidget::settings() const
{Settings s;s.legend.isEnabled =ui->cboxLegendEnabled->checkState() == Qt::Checked;s.legend.position = ui->cbxPos->currentIndex();s.legendItem.isEnabled =ui->cboxLegendItemEnabled->checkState() == Qt::Checked;s.legendItem.numColumns = ui->spinBoxColumns->value();int align = 0;int hIndex = ui->cbxHorizontal->currentIndex();if ( hIndex == 0 )align |= Qt::AlignLeft;else if ( hIndex == 2 )align |= Qt::AlignRight;elsealign |= Qt::AlignHCenter;int vIndex = ui->cbxVertical->currentIndex();if ( vIndex == 0 )align |= Qt::AlignTop;else if ( vIndex == 2 )align |= Qt::AlignBottom;elsealign |= Qt::AlignVCenter;s.legendItem.alignment = align;s.legendItem.backgroundMode =ui->cbxBackGround->currentIndex();s.legendItem.size = ui->spinBoxSize->value();s.curve.numCurves = ui->spinBoxNum->value();s.curve.title = ui->leTitle->text();return s;
}void LegendWidget::applySettings(const Settings &settings)
{m_isDirty = false;g_plot->setAutoReplot( true );//判断图列是否启用if ( settings.legend.isEnabled ){//设置图列位置if ( settings.legend.position > QwtPlot::TopLegend ){//如果有,就先删除if ( g_plot->legend() ){// remove legend controlled by the plotg_plot->insertLegend( NULL );}//弹出的图列if ( m_externalLegend == NULL ){m_externalLegend = new QwtLegend();m_externalLegend->setWindowTitle("Plot Legend");connect(g_plot,SIGNAL(legendDataChanged(const QVariant&,const QList<QwtLegendData>&)),m_externalLegend,SLOT(updateLegend(const QVariant&,const QList<QwtLegendData>&)) );m_externalLegend->show();// populate the new legendg_plot->updateLegend();}}else{delete m_externalLegend;m_externalLegend = NULL;if ( g_plot->legend() == NULL ||g_plot->plotLayout()->legendPosition() != settings.legend.position ){g_plot->insertLegend( new QwtLegend(),QwtPlot::LegendPosition( settings.legend.position ) );}}}else{g_plot->insertLegend( NULL );delete m_externalLegend;m_externalLegend = NULL;}//判断图例子项是否启用if ( settings.legendItem.isEnabled ){if ( m_legendItem == NULL ){m_legendItem = new LegendItem();m_legendItem->attach( g_plot );}//设置最大列数m_legendItem->setMaxColumns( settings.legendItem.numColumns );//设置对齐方式m_legendItem->setAlignmentInCanvas( Qt::Alignment( settings.legendItem.alignment ) );//设置背景模式m_legendItem->setBackgroundMode(QwtPlotLegendItem::BackgroundMode( settings.legendItem.backgroundMode ) );if ( settings.legendItem.backgroundMode ==QwtPlotLegendItem::ItemBackground ){m_legendItem->setBorderRadius( 4 );m_legendItem->setMargin( 0 );m_legendItem->setSpacing( 4 );m_legendItem->setItemMargin( 2 );}else{m_legendItem->setBorderRadius( 8 );m_legendItem->setMargin( 4 );m_legendItem->setSpacing( 2 );m_legendItem->setItemMargin( 0 );}//设置字体大小QFont font = m_legendItem->font();font.setPointSize( settings.legendItem.size );m_legendItem->setFont( font );}else{delete m_legendItem;m_legendItem = NULL;}//画曲线QwtPlotItemList curveList = g_plot->itemList( QwtPlotItem::Rtti_PlotCurve );if ( curveList.size() != settings.curve.numCurves ){while ( curveList.size() > settings.curve.numCurves ){QwtPlotItem* curve = curveList.takeFirst();delete curve;}for ( int i = curveList.size(); i < settings.curve.numCurves; i++ )insertCurve();}curveList = g_plot->itemList( QwtPlotItem::Rtti_PlotCurve );for ( int i = 0; i < curveList.count(); i++ ){Curve* curve = static_cast< Curve* >( curveList[i] );curve->setCurveTitle( settings.curve.title );int sz = 0.5 * settings.legendItem.size;curve->setLegendIconSize( QSize( sz, sz ) );}g_plot->setAutoReplot( false );if ( m_isDirty ){m_isDirty = false;g_plot->replot();}
}void LegendWidget::insertCurve()
{static int counter = 1;const char* colors[] ={"LightSalmon","SteelBlue","Yellow","Fuchsia","PaleGreen","PaleTurquoise","Cornsilk","HotPink","Peru","Maroon"};const int numColors = sizeof( colors ) / sizeof( colors[0] );QwtPlotCurve* curve = new Curve( counter++ );curve->setPen( QColor( colors[ counter % numColors ] ), 2 );curve->attach( g_plot );
}void LegendWidget::on_cboxLegendEnabled_stateChanged(int arg1)
{edited();
}void LegendWidget::on_cbxPos_currentIndexChanged(int index)
{edited();
}void LegendWidget::on_cboxLegendItemEnabled_stateChanged(int arg1)
{edited();
}void LegendWidget::on_cbxHorizontal_currentIndexChanged(int index)
{edited();
}void LegendWidget::on_cbxVertical_currentIndexChanged(int index)
{edited();
}void LegendWidget::on_cbxBackGround_currentIndexChanged(int index)
{edited();
}void LegendWidget::on_spinBoxSize_valueChanged(int arg1)
{edited();
}void LegendWidget::on_spinBoxNum_valueChanged(int arg1)
{edited();
}void LegendWidget::on_leTitle_textEdited(const QString &arg1)
{edited();
}void LegendWidget::edited()
{const Settings s = settings();applySettings( s);
}void LegendWidget::on_spinBoxColumns_valueChanged(int arg1)
{edited();
}

4.完整工程

https://download.csdn.net/download/wzz953200463/88479580

此工程不包含qwt的库,需自行编译。

这篇关于Qwt QwtLegend和QwtPlotLegendItem图例类详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/290025

相关文章

PHP轻松处理千万行数据的方法详解

《PHP轻松处理千万行数据的方法详解》说到处理大数据集,PHP通常不是第一个想到的语言,但如果你曾经需要处理数百万行数据而不让服务器崩溃或内存耗尽,你就会知道PHP用对了工具有多强大,下面小编就... 目录问题的本质php 中的数据流处理:为什么必不可少生成器:内存高效的迭代方式流量控制:避免系统过载一次性

MySQL的JDBC编程详解

《MySQL的JDBC编程详解》:本文主要介绍MySQL的JDBC编程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、前置知识1. 引入依赖2. 认识 url二、JDBC 操作流程1. JDBC 的写操作2. JDBC 的读操作总结前言本文介绍了mysq

Redis 的 SUBSCRIBE命令详解

《Redis的SUBSCRIBE命令详解》Redis的SUBSCRIBE命令用于订阅一个或多个频道,以便接收发送到这些频道的消息,本文给大家介绍Redis的SUBSCRIBE命令,感兴趣的朋友跟随... 目录基本语法工作原理示例消息格式相关命令python 示例Redis 的 SUBSCRIBE 命令用于订

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Python中 try / except / else / finally 异常处理方法详解

《Python中try/except/else/finally异常处理方法详解》:本文主要介绍Python中try/except/else/finally异常处理方法的相关资料,涵... 目录1. 基本结构2. 各部分的作用tryexceptelsefinally3. 执行流程总结4. 常见用法(1)多个e

SpringBoot日志级别与日志分组详解

《SpringBoot日志级别与日志分组详解》文章介绍了日志级别(ALL至OFF)及其作用,说明SpringBoot默认日志级别为INFO,可通过application.properties调整全局或... 目录日志级别1、级别内容2、调整日志级别调整默认日志级别调整指定类的日志级别项目开发过程中,利用日志

Java中的抽象类与abstract 关键字使用详解

《Java中的抽象类与abstract关键字使用详解》:本文主要介绍Java中的抽象类与abstract关键字使用详解,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、抽象类的概念二、使用 abstract2.1 修饰类 => 抽象类2.2 修饰方法 => 抽象方法,没有

MySQL8 密码强度评估与配置详解

《MySQL8密码强度评估与配置详解》MySQL8默认启用密码强度插件,实施MEDIUM策略(长度8、含数字/字母/特殊字符),支持动态调整与配置文件设置,推荐使用STRONG策略并定期更新密码以提... 目录一、mysql 8 密码强度评估机制1.核心插件:validate_password2.密码策略级

从入门到精通详解Python虚拟环境完全指南

《从入门到精通详解Python虚拟环境完全指南》Python虚拟环境是一个独立的Python运行环境,它允许你为不同的项目创建隔离的Python环境,下面小编就来和大家详细介绍一下吧... 目录什么是python虚拟环境一、使用venv创建和管理虚拟环境1.1 创建虚拟环境1.2 激活虚拟环境1.3 验证虚

详解python pycharm与cmd中制表符不一样

《详解pythonpycharm与cmd中制表符不一样》本文主要介绍了pythonpycharm与cmd中制表符不一样,这个问题通常是因为PyCharm和命令行(CMD)使用的制表符(tab)的宽... 这个问题通常是因为PyCharm和命令行(CMD)使用的制表符(tab)的宽度不同导致的。在PyChar