#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 );
//创建plot
g_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;
else
align |= Qt::AlignHCenter;
int vIndex = ui->cbxVertical->currentIndex();
if ( vIndex == 0 )
align |= Qt::AlignTop;
else if ( vIndex == 2 )
align |= Qt::AlignBottom;
else
align |= 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 plot
g_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 legend
g_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] );
没有合适的资源?快使用搜索试试~ 我知道了~
qwt QwtLegend示例
共9个文件
cpp:3个
h:2个
ui:2个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 166 浏览量
2023-10-27
21:15:50
上传
评论
收藏 11KB ZIP 举报
温馨提示
qwt QwtLegend示例qwt QwtLegend示例qwt QwtLegend示例qwt QwtLegend示例qwt QwtLegend示例qwt QwtLegend示例qwt QwtLegend示例qwt QwtLegend示例qwt QwtLegend示例qwt QwtLegend示例qwt QwtLegend示例qwt QwtLegend示例qwt QwtLegend示例qwt QwtLegend示例qwt QwtLegend示例qwt QwtLegend示例qwt QwtLegend示例qwt QwtLegend示例qwt QwtLegend示例qwt QwtLegend示例qwt QwtLegend示例qwt QwtLegend示例qwt QwtLegend示例qwt QwtLegend示例qwt QwtLegend示例qwt QwtLegend示例qwt QwtLegend示例qwt QwtLegend示例qwt 没有qwt的库,自行编译。
资源推荐
资源详情
资源评论
收起资源包目录
untitled.zip (9个子文件)
untitled
LegendWidget.h 2KB
mainwindow.h 313B
mainwindow.cpp 4KB
LegendWidget.cpp 10KB
untitled.pro 1KB
main.cpp 210B
mainwindow.ui 1KB
LegendWidget.ui 14KB
untitled.pro.user 23KB
共 9 条
- 1
资源评论
Mr.codeee
- 粉丝: 3w+
- 资源: 59
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功