技术专栏
qt图表库qcustomplot使用心得记录三 (选区取点)
qt图表库qcustomplot使用心得记录三 (选区取点)
本系列讲述的是我使用qt图表库qcustomplot中的使用心得分享,借此记录我的学习内容,也希望可以给与初学者一些帮助。
本篇文章讲述的是实现选取、获取选取内数据的功能,虽然qcustomplot自带选取功能,但是感觉不太好用(可能是不太会用的原因吧),所以就自己封装了选取功能。
1.实现原理
选区功能的实现原理非常简单,就是使用橡皮筋选择区域,取选区内数据点是通过橡皮筋的选择范围坐标获取在坐标内的数据。
代码实例:
代码中没有用到多么复杂的函数,一些QCustomPlot的函数第二篇文章都有介绍
- 1
- 2
- 3
#ifndef CUSTONPLOTEX_H
#define CUSTONPLOTEX_H
#include <QWidget>
#include "QCustomPlot/qcustomplot.h"
#include <QRubberBand>
#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<double> selectionRectGraphKey(int index);
QVector<double> selectionRectGraphKey(QCPGraph *graph);
//选区内图表value
QVector<double> selectionRectGraphValue(int index);
QVector<double> selectionRectGraphValue(QCPGraph *graph);
//选区内图表所有的点的值
QMap<double,double> selectionRectGraphData(int index);
QMap<double,double> 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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
<
#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<int>(xAxis->coordToPixel(startKey));
startPos.ry() = 0;
QPoint endPos;
endPos.rx() = static_cast<int>(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<double> CustomPlotEx::selectionRectGraphKey(int index)
{
QVector<double> 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<double> CustomPlotEx::selectionRectGraphKey(QCPGraph *graph)
{
QVector<double> 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<double> CustomPlotEx::selectionRectGraphValue(int index)
{
QVector<double> 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<double> CustomPlotEx::selectionRectGraphValue(QCPGraph *graph)
{
QVector<double> 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<double, double> CustomPlotEx::selectionRectGraphData(int index)
{
QMap<double,double> 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<double, double> CustomPlotEx::selectionRectGraphData(QCPGraph *graph)
{
QMap<double,double> 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<QCPLayerable*> 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*)));
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
<
声明:本文内容由易百纳平台入驻作者撰写,文章观点仅代表作者本人,不代表易百纳立场。如有内容侵权或者其他问题,请联系本站进行删除。
红包
点赞
收藏
评论
打赏
- 分享
- 举报
评论
0个
手气红包

相关专栏
-
浏览量:7639次2020-08-23 21:25:35
-
浏览量:5684次2020-08-23 21:07:51
-
浏览量:2970次2020-08-23 20:59:44
-
浏览量:5437次2020-05-08 15:46:11
-
浏览量:3576次2020-05-07 19:43:16
-
浏览量:7744次2020-12-11 16:08:02
-
浏览量:1992次2018-12-25 15:48:04
-
浏览量:6839次2018-01-22 14:23:15
-
浏览量:2882次2020-04-24 17:44:09
-
浏览量:2045次2020-08-28 16:40:19
-
浏览量:1011次2023-12-11 16:43:29
-
浏览量:2795次2017-12-03 14:42:30
-
浏览量:5244次2020-12-19 17:48:05
-
2024-02-04 10:33:53
-
浏览量:7838次2020-11-25 22:46:18
-
浏览量:2289次2022-03-08 09:00:11
-
浏览量:2495次2020-08-19 18:24:06
-
浏览量:2851次2020-06-15 10:45:14
-
浏览量:1251次2023-07-05 10:11:08
置顶时间设置
结束时间
删除原因
-
广告/SPAM
-
恶意灌水
-
违规内容
-
文不对题
-
重复发帖
打赏作者

小王子🤴
您的支持将鼓励我继续创作!
打赏金额:
¥1

¥5

¥10

¥50

¥100

支付方式:

举报反馈
举报类型
- 内容涉黄/赌/毒
- 内容侵权/抄袭
- 政治相关
- 涉嫌广告
- 侮辱谩骂
- 其他
详细说明
审核成功
发布时间设置
发布时间:
请选择发布时间设置
是否关联周任务-专栏模块
审核失败
失败原因
请选择失败原因
备注
请输入备注