鼠标悬停时显示绘图值.-检测散射点
Display the plot values on mouse over. - Detect Scatter points
我正在尝试显示QCustomPlot
上不同点的绘图值,其中我的线样式为lsLine
。我知道我可以在QCustomPlot
上设置鼠标悬停信号,但这并没有真正的帮助,因为我只需要在鼠标悬停在我绘制的线上时得到通知。我的问题是,有没有办法找出鼠标是否在我的散射点上。有没有一个信号可以连接到,告诉我鼠标何时在散射点上?
重新实现QCustomPlot::mouseMoveEvent
或连接到QCustomPlot::mouseMove
。
然后使用轴的coordToPixel
平移(光标)像素坐标以绘制坐标,并使用QMap::lowerBound(cursorX)
搜索QCPDataMap
中最近的点。
您可以轻松地将插槽连接到QCustomPlot
发出的mouseMove
信号。然后,您可以使用QCPAxis::pixelToCoord
查找坐标:
connect(this, SIGNAL(mouseMove(QMouseEvent*)), this,SLOT(showPointToolTip(QMouseEvent*)));
void QCustomPlot::showPointToolTip(QMouseEvent *event)
{
int x = this->xAxis->pixelToCoord(event->pos().x());
int y = this->yAxis->pixelToCoord(event->pos().y());
setToolTip(QString("%1 , %2").arg(x).arg(y));
}
当您使用X轴的日期时间格式(包括每秒更多点)时,像素坐标失败。如果你想显示点之间的坐标,那么这是最快的方法
可能已满(连接信号QCustomplot::MouseMove
)
void MainWindow::onMouseMoveGraph(QMouseEvent* evt)
{
int x = this->ui->customPlot->xAxis->pixelToCoord(evt->pos().x());
int y = this->ui->customPlot->yAxis->pixelToCoord(evt->pos().y());
qDebug()<<"pixelToCoord: "<<data.key<<data.value; //this is correct when step is greater 1 second
if (this->ui->customPlot->selectedGraphs().count()>0)
{
QCPGraph* graph = this->ui->customPlot->selectedGraphs().first();
QCPData data = graph->data()->lowerBound(x).value();
double dbottom = graph->valueAxis()->range().lower; //Yaxis bottom value
double dtop = graph->valueAxis()->range().upper; //Yaxis top value
long ptop = graph->valueAxis()->axisRect()->top(); //graph top margin
long pbottom = graph->valueAxis()->axisRect()->bottom(); //graph bottom position
// result for Y axis
double valueY = (evt->pos().y() - ptop) / (double)(pbottom - ptop)*(double)(dbottom - dtop) + dtop;
//or shortly for X-axis
double valueX = (evt->pos().x() - graph->keyAxis()->axisRect()->left()); //graph width in pixels
double ratio = (double)(graph->keyAxis()->axisRect()->right() - graph->keyAxis()->axisRect()->left()) / (double)(graph->keyAxis()->range().lower - graph->keyAxis()->range().upper); //ratio px->graph width
//and result for X-axis
valueX=-valueX / ratio + graph->keyAxis()->range().lower;
qDebug()<<"calculated:"<<valueX<<valueY;
}
}
相关文章:
- 使用CMake检测支持的C++标准
- 当套接字连接断开时检测C/C++Unix
- C/C++预处理器是否可以检测一些编译器选项
- WMI检测进程创建事件-c++
- 基于树莓pi的tensorflow lite量化ssd目标检测
- 下面是我为检测链接列表中的循环而制作的代码
- 落砂模拟碰撞检测C++和SFML
- 我可以检测和更改 gcc/g++ 中结构的当前数据对齐设置吗?
- 为什么C++编译器没有检测到正确声明的类?
- 将鼠标悬停在问题上时与预期">"相关的代码错误
- 检测win32服务创建和删除的最佳方法
- 正在LLVM中检测整数比较条件
- 如何在鼠标挂钩过程中检测拖动
- 位移操作和位掩码未检测到重复字符
- 检测 COFF 对象文件中C++内联符号
- 如何检测将鼠标悬停在静态 Win32 控件上
- Winapi 检测按钮悬停
- 在SFML中检测鼠标悬停的有效方法
- 鼠标悬停时显示绘图值.-检测散射点
- 如何在OpenGL中检测鼠标悬停在某个对象上