qt图表库qcustomplot使用心得记录三 (选区取点)

qt图表库qcustomplot使用心得记录三 (选区取点)

本系列讲述的是我使用qt图表库qcustomplot中的使用心得分享,借此记录我的学习内容,也希望可以给与初学者一些帮助。

本篇文章讲述的是实现选取、获取选取内数据的功能,虽然qcustomplot自带选取功能,但是感觉不太好用(可能是不太会用的原因吧),所以就自己封装了选取功能。

1.实现原理

选区功能的实现原理非常简单,就是使用橡皮筋选择区域,取选区内数据点是通过橡皮筋的选择范围坐标获取在坐标内的数据。

代码实例:

代码中没有用到多么复杂的函数,一些QCustomPlot的函数第二篇文章都有介绍

#ifndef CUSTONPLOTEX_H

#define CUSTONPLOTEX_H

#include

#include “QCustomPlot/qcustomplot.h”

#include

#include “stdio.h”

class CustomPlotEx : public QCustomPlot

{

Q_OBJECT

public:

CustomPlotEx();

CustomPlotEx(QWidget *widget);

~CustomPlotEx() Q_DECL_OVERRIDE;

//打开选区功能

//关闭选区功能

void openSeleteArea();

void closeSeleteArea();

bool seleteAreaState();//获取当前选区功能状态

//锁定,解锁

void lock();

void unLock();

//自动选取

void selectionRectFromRange(double startKey,double endKey);

//选区范围值

QCPRange selectionRectAxis_X();

QCPRange selectionRectAxis_Y();

//参数是否是选区内的key

bool isSelectionRectKey(double key);

//参数是否是选区内的value

bool isSelectionRectValue(double value);

/********************************选区内图表数据**************************************/

//选区内图表key

QVector selectionRectGraphKey(int index);

QVector selectionRectGraphKey(QCPGraph *graph);

//选区内图表value

QVector selectionRectGraphValue(int index);

QVector selectionRectGraphValue(QCPGraph *graph);

//选区内图表所有的点的值

QMap selectionRectGraphData(int index);

QMap selectionRectGraphData(QCPGraph *graph);

//复位选区

void resetSelectionRect();

protected:

//键盘按下事件

virtual void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE;

virtual void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;

virtual void mouseDoubleClickEvent(QMouseEvent *event) Q_DECL_OVERRIDE;

virtual void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;

virtual void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;

virtual void wheelEvent(QWheelEvent *event) Q_DECL_OVERRIDE;

virtual void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;

public slots:

//鼠标按下

void mousePress(QMouseEvent *event);

//鼠标移动

void mouseMove(QMouseEvent *event);

//鼠标释放

void mouseRelease(QMouseEvent *event);

//鼠标滑轮

void mouseWheel(QWheelEvent *event);

private:

void Init();

//选区

QRubberBand *rb;

QPoint startPos;//选区开始

bool cancelRb = false;

bool isSeleteArea = false;//是否选区

bool isLock = false;//锁定

};

#endif // CUSTONPLOTEX_H
<

#include “customplotex.h”

CustomPlotEx::CustomPlotEx() : QCustomPlot(new QWidget())

{

Init();

}

CustomPlotEx::CustomPlotEx(QWidget *widget) : QCustomPlot(widget)

{

Init();

}

CustomPlotEx::~CustomPlotEx()

{

}

void CustomPlotEx::openSeleteArea()

{

isSeleteArea = true;

}

void CustomPlotEx::closeSeleteArea()

{

resetSelectionRect();

isSeleteArea = false;

}

bool CustomPlotEx::seleteAreaState()

{

return isSeleteArea;

}

void CustomPlotEx::lock()

{

isLock = true;

}

void CustomPlotEx::unLock()

{

isLock = false;

}

void CustomPlotEx::selectionRectFromRange(double startKey, double endKey)

{

startPos.rx() = static_cast(xAxis->coordToPixel(startKey));

startPos.ry() = 0;

QPoint endPos;

endPos.rx() = static_cast(xAxis->coordToPixel(endKey));

endPos.ry() = this->height();

resetSelectionRect();

QRect normalRect = QRect(startPos, endPos).normalized();//任意两点定义矩形

rb->setGeometry(normalRect);

rb->show();

}

QCPRange CustomPlotEx::selectionRectAxis_X()

{

// qDebug() << “rb:” << rb->x() << “,” << rb->x()+rb->rect().width();

return QCPRange(xAxis->pixelToCoord(rb->x()), xAxis->pixelToCoord(rb->x()+rb->rect().width()));

}

QCPRange CustomPlotEx::selectionRectAxis_Y()

{

// qDebug() << “rb->y()-rb->rect().height():” << rb->y()-rb->rect().height() << “rb->y():” << rb->y();

return QCPRange(yAxis->pixelToCoord(rb->y()+rb->rect().height()), yAxis->pixelToCoord(rb->y()));

}

bool CustomPlotEx::isSelectionRectKey(double key)

{

QCPRange rang = selectionRectAxis_X();

return (key >= rang.lower && key <= rang.upper);

}

bool CustomPlotEx::isSelectionRectValue(double value)

{

QCPRange rang = selectionRectAxis_Y();

return (value >= rang.lower && value <= rang.upper);

}

QVector CustomPlotEx::selectionRectGraphKey(int index)

{

QVector keyVector;

for (auto i = graph(index)->data().data()->begin();i != graph(index)->data().data()->end();i++)

{

if(isSelectionRectKey(i->key))

{

keyVector.append(i->key);

}

}

return keyVector;

}

QVector CustomPlotEx::selectionRectGraphKey(QCPGraph *graph)

{

QVector keyVector;

for (auto i = graph->data().data()->begin();i != graph->data().data()->end();i++)

{

if(isSelectionRectKey(i->key))

{

keyVector.append(i->key);

}

}

return keyVector;

}

QVector CustomPlotEx::selectionRectGraphValue(int index)

{

QVector valueVector;

for (auto i = graph(index)->data().data()->begin();i != graph(index)->data().data()->end();i++)

{

if(isSelectionRectValue(i->value))

{

valueVector.append(i->value);

}

}

return valueVector;

}

QVector CustomPlotEx::selectionRectGraphValue(QCPGraph *graph)

{

QVector valueVector;

for (auto i = graph->data().data()->begin();i != graph->data().data()->end();i++)

{

if(isSelectionRectValue(i->value))

{

valueVector.append(i->value);

}

}

return valueVector;

}

QMap CustomPlotEx::selectionRectGraphData(int index)

{

QMap graphData;

for (auto i = graph(index)->data().data()->begin();i != graph(index)->data().data()->end();i++)

{

if(isSelectionRectKey(i->key) && isSelectionRectValue(i->value))

{

// qDebug() << i->key;

graphData.insert(i->key,i->value);

}

}

return graphData;

}

QMap CustomPlotEx::selectionRectGraphData(QCPGraph *graph)

{

QMap graphData;

for (auto i = graph->data().data()->begin();i != graph->data().data()->end();i++)

{

if(isSelectionRectKey(i->key) && isSelectionRectValue(i->value))

{

graphData.insert(i->key,i->value);

}

}

return graphData;

}

void CustomPlotEx::keyPressEvent(QKeyEvent *event)

{

if(isLock)

return;

if(cancelRb)

{

if(event->key() == Qt::Key_Up)

{

}else if (event->key() == Qt::Key_Down) {

}else if (event->key() == Qt::Key_Left) {

rb->move(rb->x()-1,rb->y());

}else if (event->key() == Qt::Key_Right) {

rb->move(rb->x()+1,rb->y());

}

}

}

void CustomPlotEx::mousePressEvent(QMouseEvent *event)

{

if(isLock)

return;

if(isSeleteArea)

{

/*gyh add start*/

if((event->buttons() & Qt::RightButton) || (event->buttons() & Qt::LeftButton))

{

if(cancelRb)

{

if(event->pos().x() >= rb->x() &&

event->pos().y() >= rb->y() &&

event->pos().x() <= rb->x() + rb->width() &&

event->pos().y() <= rb->y() + rb->height())

{

cancelRb = true;

startPos = event->pos();

goto other;

}

}

resetSelectionRect();

startPos.rx() = event->pos().rx();

startPos.ry() = 0;

// startPos = event->pos();

rb->show();

other:;

}

/*gyh add end*/

replot();

}else{

QCustomPlot::mousePressEvent(event);

}

}

void CustomPlotEx::mouseDoubleClickEvent(QMouseEvent *event)

{

if(isLock)

return;

QCustomPlot::mouseDoubleClickEvent(event);

}

void CustomPlotEx::mouseMoveEvent(QMouseEvent *event)

{

if(isLock)

return;

QCustomPlot::mouseMoveEvent(event);

}

void CustomPlotEx::mouseReleaseEvent(QMouseEvent *event)

{

if(isLock)

return;

QCustomPlot::mouseReleaseEvent(event);

}

void CustomPlotEx::wheelEvent(QWheelEvent *event)

{

if(isLock)

return;

QCustomPlot::wheelEvent(event);

}

void CustomPlotEx::paintEvent(QPaintEvent *event)

{

QCustomPlot::paintEvent(event);

}

void CustomPlotEx::mousePress(QMouseEvent *event)

{

Q_UNUSED(event);

}

void CustomPlotEx::mouseMove(QMouseEvent *event)

{

QList candidates = layerableListAt(event->pos(), false);

if(candidates.contains(this->xAxis))

{

setCursor(Qt::SizeHorCursor);

}else if(candidates.contains(this->yAxis))

{

setCursor(Qt::SizeVerCursor);

}else{

setCursor(Qt::ArrowCursor);

}

/*gyh add start*/

if(cancelRb)

{

if((event->buttons() & Qt::RightButton) || (event->buttons() & Qt::LeftButton))

{

clearAverageGraph();

QPoint movePos = event->pos() – startPos;

startPos = event->pos();

rb->move(rb->x() + movePos.x(),rb->y() /*+ movePos.y()*/);

return;

}

}

if(event->buttons() & Qt::RightButton || event->buttons() & Qt::LeftButton)

{

QPoint endPos;

endPos.rx() = event->pos().rx();

endPos.ry() = this->height();

QRect normalRect = QRect(startPos, endPos).normalized();//任意两点定义矩形

rb->setGeometry(normalRect);

}

/*gyh add end*/

}

void CustomPlotEx::mouseRelease(QMouseEvent *event)

{

/*gyh add start*/

if((event->button() == Qt::RightButton) || (event->button() == Qt::LeftButton))

{

if(cancelRb)

{

return;

}

//起始位置与结束位置相同取消选区

if(startPos == event->pos())

{

cancelRb = false;

rb->resize(0, 0);

rb->hide();

}

cancelRb = true;

}

/*gyh add end*/

}

void CustomPlotEx::mouseWheel(QWheelEvent *event)

{

Q_UNUSED(event);

resetSelectionRect();

}

void CustomPlotEx::Init()

{

// setAntialiasedElement(QCP::aeAll);//抗锯齿

setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes |

QCP::iSelectLegend | QCP::iSelectPlottables);

setPlottingHints(QCP::phFastPolylines | QCP::phImmediateRefresh | QCP::phCacheLabels);

rb = new QRubberBand(QRubberBand::Rectangle, this);

rb->resize(0,0);

startPos = QPoint(0, 0);

this->setOpenGl(true);

setNoAntialiasingOnDrag(true);

connect(this, SIGNAL(mousePress(QMouseEvent*)), this, SLOT(mousePress(QMouseEvent*)));

connect(this, SIGNAL(mouseMove(QMouseEvent*)), this, SLOT(mouseMove(QMouseEvent*)));

connect(this, SIGNAL(mouseRelease(QMouseEvent*)), this, SLOT(mouseRelease(QMouseEvent*)));

connect(this, SIGNAL(mouseWheel(QWheelEvent*)), this, SLOT(mouseWheel(QWheelEvent*)));

}

<

免责声明:文章内容来自互联网,本站不对其真实性负责,也不承担任何法律责任,如有侵权等情况,请与本站联系删除。
转载请注明出处:qt图表库qcustomplot使用心得记录三 (选区取点) https://www.yhzz.com.cn/a/14919.html

上一篇 2023-05-12 12:30:37
下一篇 2023-05-12 12:32:44

相关推荐

联系云恒

在线留言: 我要留言
客服热线:400-600-0310
工作时间:周一至周六,08:30-17:30,节假日休息。