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的库,需自行编译。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/151152.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

如何在Windows和Linux系统上监听文件夹的变动?

文章目录 如何在Windows和Linux系统上监听文件夹的变动&#xff1f;读写文件文件系统的操作缓冲和流文件改变事件 如何在Windows和Linux系统上监听文件夹的变动&#xff1f; libuv库实现了监听整个文件夹的修改。本文详细介绍libuv库文件读写和监听的的实现方法。libuv库开发了…

阿里云对象存储OSS文件无法预览,Bucket设置了Referer

您发起的请求头中没有Referer字段或Referer字段为空&#xff0c;与请求Bucket设置的防盗链策略不相符。 解决方案 您可以选择以下任意方案解决该问题。 在请求中增加Referer请求头。 GET /test.txt HTTP/1.1 Date: Tue, 20 Dec 2022 08:48:18 GMT Host: BucketName.oss-examp…

存储器概述

一、存储系统基本概念

『Linux』补丁制作指南

前言 我们在参与某些开源项目的过程当中&#xff0c;经常会遇到漏洞之类的问题&#xff0c;需要我们打补丁解决。尤其是 Linux 源码&#xff0c;源码代码量较多&#xff0c;在修改完内核并发布新内核的时候&#xff0c;基本采用补丁的方式进行发布&#xff0c;而不是将整个内核…

修炼k8s+flink+hdfs+dlink(六:学习namespace,service)

一&#xff1a;什么是namespace&#xff1f; 你可以认为namespaces是你kubernetes集群中的虚拟化集群。在一个Kubernetes集群中可以拥有多个命名空间&#xff0c;它们在逻辑上彼此隔离。 他们可以为您和您的团队提供组织&#xff0c;安全甚至性能方面的帮助&#xff01; 二&a…

QGIS008:QGIS拓扑检查、修改及验证

摘要&#xff1a;本文介绍使用QGIS拓扑检查器和几何图形检查器检查图层的拓扑错误&#xff0c;修改拓扑错误&#xff0c;并对修改后的图层进行错误验证。 实验数据&#xff1a; 链接&#xff1a;https://pan.baidu.com/s/1Vy2s-KYS-XJevqHNdavv9A?pwdf06o 提取码&#xff1a…

Linux学习第24天:Linux 阻塞和非阻塞 IO 实验(一): 挂起

Linux版本号4.1.15 芯片I.MX6ULL 大叔学Linux 品人间百味 思文短情长 在正式开始今天的笔记之前谈一下工作中遇见的一个问题。 本篇笔记主要学习Linux 阻塞和非阻塞 IO 实验&#xff0c;主要包括阻塞和非阻塞简介、等待队列、轮询、…

适用于嵌入式arm的ffmpeg编解码

在嵌入式arm应用开发中&#xff0c;经常会遇到需要处理视频的情况&#xff0c;这时候就需要强大的开源工具ffmpeg出马了。 这里可以下载到各个版本的ffmpeg。 ffmpeg各版本https://www.videohelp.com/software/ffmpeg/old-versions 现在ffmpeg更新较频繁&#xff0c;如…

uniapp把文件中的内复制到另一个文件中

使用的是Html 5的plus.io.resolveLocalFileSystemURL方法&#xff0c;文档&#xff1a;HTML5 API Reference var soursePath file:///storage/emulated/0/a/;//用于读取var removePath file:///storage/emulated/0/w/;//用于移除w这个文件夹var targetPath file:///storage/…

STM32H750之FreeRTOS学习--------(一)初识RTOS

FreeRTOS 一、初识RTOS 裸机&#xff1a;裸机又称为前后台系统&#xff0c;前台系统指的中断服务函数&#xff0c;后台系统指的大循环&#xff0c;即应用程序 实时性差,程序轮流执行delayCPU空等待&#xff0c;效率低程序混乱&#xff0c;臃肿&#xff0c;功能都放在while循环…

Unity报错:Microsoft Visual C# Compiler version

Unity报错:Microsoft Visual C# Compiler version 问题解决方案总结 问题 Microsoft Visual C# Compiler version 2.9.1.65535 (9d34608e) Copyright © Microsoft Corporation 切换版本或者使用老项目的时候可能会出现这个报错&#xff0c;这个报错就是项目设置的问题 …

一款集成了主流大语言模型以及绘图模型的 APP, 采用 Flutter 开发,代码完全开源!!

一款集成了主流大语言模型以及绘图模型的 APP&#xff0c; 采用 Flutter 开发&#xff0c;代码完全开源&#xff0c;支持以下功能&#xff1a; 支持 OpenAI 的 GPT-3.5&#xff0c;GPT-4 大语言模型支持 Anthropic 的 Claude instant&#xff0c;Claude 2.0 大语言模型支持国产…