QCustomPlot 的使用 散点图 升级版(菜单、图例、鼠标显示值)

QCustomPlot 的使用 散点图

前沿:在自身不断的努力下,对散点图进行了优化
设计到的知识点

  1. 散点的绘制,值得注意的是,如果需要绘制多层的话,需要加入customplot->addGraph();// 增加图层
    不然会报错,错误如下图。
    在这里插入图片描述
    代码如下:
// 设置画笔风格
	QPen drawPen;
	drawPen.setColor(Qt::red);
	drawPen.setWidth(4);

	// 绘制散点
	QCPGraph * curGraph = customplot->graph(0);
	curGraph->setPen(drawPen);
	curGraph->setLineStyle(QCPGraph::lsNone);
	curGraph->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 2));//点是空心的
	//ssDisc 点是实心的,需要改变参数即可
	customplot->graph(0)->setName(QString::fromLocal8Bit("你是憨批"));// 图例的名字
	
// 设置画笔风格
	QPen drawPen1;
	drawPen1.setColor(Qt::black);
	drawPen1.setWidth(4);

	// 绘制散点
	customplot->addGraph();
	QCPGraph * curGraph1 = customplot->graph(1);
	curGraph1->setPen(drawPen1);
	curGraph1->setLineStyle(QCPGraph::lsNone);//曲线样式
	curGraph1->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 2));//点是空心的
	customplot->graph(1)->setName(QString::fromLocal8Bit("我是耙耳朵"));// 图例的名字
  1. 图例的添加
    customplot->graph(1)->setName(QString::fromLocal8Bit(“我是耙耳朵”));// 设置图例的名字,我把这句代码放在前面了
	customplot->legend->setVisible(true); //设置图例是否可用
	QFont legendFont = font();
	legendFont.setPointSize(10);
	customplot->legend->setFont(legendFont);
	customplot->legend->setSelectedFont(legendFont);
	customplot->legend->setSelectableParts(QCPLegend::spItems); // legend box shall not be selectable, only legend items
	
//后面的为补充,可以不加
	//设置图例图标大小
	customplot->legend->setIconSize(5, 5);

	//设置图例文字颜色
	customplot->legend->setTextColor(Qt::red);//设置图例文字颜色

	//在图例中添加线
	customplot->axisRect()->setupFullAxesBox();

	//将图例矩形域放到右上角
	customplot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignTop | Qt::AlignRight);

	//设置图例背景色
	customplot->legend->setBrush(QColor(255, 255, 255, 0));//设置背景色
  1. 添加图的标题
	customplot->plotLayout()->insertRow(0);
	QCPTextElement *title = new QCPTextElement(customplot, "my love chart", QFont("sans", 17, QFont::Bold));
	customplot->plotLayout()->addElement(0, 0, title);
  1. 菜单的添加(实现的是对图例位置的设置) 如果想右键图出现菜单,则注释if语句
	customplot->setContextMenuPolicy(Qt::CustomContextMenu);
	connect(customplot, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextMenuRequest(QPoint)));
	
//槽函数的编写
//菜单的生成
void CustomPoltWidget::contextMenuRequest(QPoint pos)
{
	QMenu *menu = new QMenu(this);
	menu->setAttribute(Qt::WA_DeleteOnClose);
 	if (customplot->legend->selectTest(pos, false) >= 0) // context menu on legend requested
 	{
		menu->addAction("Move to top left", this, SLOT(moveLegend()))->setData((int)(Qt::AlignTop | Qt::AlignLeft));
		menu->addAction("Move to top center", this, SLOT(moveLegend()))->setData((int)(Qt::AlignTop | Qt::AlignHCenter));
		menu->addAction("Move to top right", this, SLOT(moveLegend()))->setData((int)(Qt::AlignTop | Qt::AlignRight));
		menu->addAction("Move to bottom right", this, SLOT(moveLegend()))->setData((int)(Qt::AlignBottom | Qt::AlignRight));
		menu->addAction("Move to bottom left", this, SLOT(moveLegend()))->setData((int)(Qt::AlignBottom | Qt::AlignLeft));
	}
	menu->popup(customplot->mapToGlobal(pos));
}
//图例的移动
void CustomPoltWidget::moveLegend()
{
	if (QAction* contextAction = qobject_cast<QAction*>(sender())) // make sure this slot is really called by a context menu action, so it carries the data we need
	{
		bool ok;
		int dataInt = contextAction->data().toInt(&ok);
		if (ok)
		{
			customplot->axisRect()->insetLayout()->setInsetAlignment(0, (Qt::Alignment)dataInt);
			customplot->replot();
		}
	}
}

效果图

在这里插入图片描述
在这里插入图片描述

  • 2
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

帅气转身而过

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

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

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

打赏作者

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

抵扣说明:

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

余额充值