Qt-QCustomPlot 类

QCustomPlot

简单介绍:

第三方类,主要用于绘图

使用说明

官网:https://www.qcustomplot.com;
在这里插入图片描述
从官网下载后解压。examples 里面有一些简单的例子,可以参考。
注意:自己使用的时候需要引用qcustomplot.cpp 和 qucstomplot.h 两个文件

customPlot = new QCustomPlot(this);

基本属性介绍

QCustomPlot *cud;
1.	设置x,y轴范围
	cud->xAxis->setRange(-8, 8);//X轴范围
	cud->yAxis->setRange(-8, 8);
2.	设置x,y轴上标签上内容
	cud->xAxis->setLabel(QString::fromLocal8Bit("时间"));//X轴的名称
	cud->yAxis->setLabel(QString::fromLocal8Bit("米(m)"));//Y轴的名称
3.	图例
	QFont lenm = font();
	lenm.setPointSize(10);//曲线说明的字体大小
	cud->legend->setFont(lenm);//使字体设置生效
4.	添加图层和给曲线命名
	cud->addGraph();//几条线,添加几个图层
	cud->graph(0)->setName(QString::fromLocal8Bit("卫星位置 X"));//给曲线命名
5.	设置标题
	cud->plotLayout()->insertRow(0);
	QCPTextElement *title = new QCPTextElement(cud, "The Internet", QFont("sans", 17, QFont::Bold));
	cud->plotLayout()->addElement(0, 0, title);
6.	设置曲线样式
	cud->graph(0)->setScatterStyle(QCPScatterStyle((QCPScatterStyle::ScatterShape)(2)));
	//2为x标记,3为一个竖扛,4为空心圆,5为实心圆,6为空心正方形,7为空心棱形,8为*,9为正空心三角形,10为倒空心三角形,11为里面带x的正方形,12为“田”,13,带x的圆
7.	设置曲线颜色
	QPen ged;
	ged.setColor(QColor(Qt::blue));/蓝色
	ged.setStyle(Qt::SolidLine);//实线
	ged.setWidthF(1);
	cud->graph(0)->setPen(ged);//使用画笔绘制曲线
8.	设置缩放,拖拽
	cud->setInteractions(QCP::iSelectPlottables | QCP::iRangeDrag | QCP::iRangeZoom);//实现移动和缩放
9.	自动设置显示界面的范围
	cud->graph(0)->rescaleAxes();//自动设置显示界面的范围

菜单设置

cud->setContextMenuPolicy(Qt::CustomContextMenu);
信号量与槽函数
connect(cud, SIGNAL(customContextMenuRequested(QPoint)), this, 
SLOT(contextMenuRequest(QPoint)));
//槽函数  
void Line_chart::contextMenuRequest(QPoint pos)
{
	QTextCodec * de = QTextCodec::codecForName("GBK");
	QMenu *menu = new QMenu(this);
	menu->setAttribute(Qt::WA_DeleteOnClose);

 // general context menu on graphs requested
	menu->addAction(de->toUnicode("恢复"), this, SLOT(slotBtn()));
	menu->addAction(de->toUnicode("保存图片"), this, SLOT(SavePng()));

	menu->popup(cud->mapToGlobal(pos));
}

其他介绍

  1. 重置函数
void Line_chart::slotBtn()
{
	cud->rescaleAxes(true);
	cud->replot();
}
  1. 保存照片
void Line_chart::SavePng()
{
	QTextCodec * dss = QTextCodec::codecForName("GBK");
	//保存照片
	QString path = "D:/img";
	int i = 0;
	bool bu;
	QDir dir(path);
	if (!dir.exists())
	{
		dir.mkdir(path);
	}
	path.append("/image.png");
	QFile file(path);
	if (!file.exists())
	{
		bool bu = cud->savePng(path);
	}
	else 
	{
		path = QString("D:/img/image%1.png").arg(i++);
		bool bu = cud->savePng(path);
	}
	if (bu)
	{
		QMessageBox::information(NULL, dss->toUnicode("提示"), dss->toUnicode("保存成功"));
	}
	cud->replot();
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气转身而过

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值