I am attempting to display the plot values of different points on my QCustomPlot
in which I have a Line style of lsLine
. I know i could set a mouse over signal on the QCustomPlot
but that wont really help since I just need to be informed when the mouse is over my plotted line.My question is is there any way to find out if the mouse is over my scatter point. Is there a signal i could connect to that would tell me when the mouse is over a scatter point ?
我试图在我的QCustomPlot中显示不同点的绘图值,其中我有一个lsLine的线型。我知道我可以在QCustomPlot上设置一个鼠标悬停信号,但这不会真正有用,因为我只需要在鼠标悬停在我绘制的线上时得到通知。我的问题是有没有办法找出鼠标是否超过我的分散点。是否有可以连接的信号,当鼠标在散点上时会告诉我?
3 个解决方案
#1
9
Reimplement QCustomPlot::mouseMoveEvent
or connect to QCustomPlot::mouseMove
.
重新实现QCustomPlot :: mouseMoveEvent或连接到QCustomPlot :: mouseMove。
Then use axes' coordToPixel
to translate (cursor) pixel coords to plot coords and search nearest points in your QCPDataMap
with QMap::lowerBound(cursorX)
.
然后使用axis的coordToPixel转换(光标)像素坐标以绘制坐标并使用QMap :: lowerBound(cursorX)搜索QCPDataMap中的最近点。
#2
7
You can easily just connect a slot to the mouseMove
signal that QCustomPlot
emits. You can then use QCPAxis::pixelToCoord
to find the coordinate :
您可以轻松地将插槽连接到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));
}
#3
2
when You use datetime format (including more point per second) of X axis, then pixel to coord fails. If you want to display coordinates between points, then this is the fastest way
当您使用X轴的日期时间格式(包括每秒更多点数)时,像素到坐标将失败。如果要显示点之间的坐标,那么这是最快的方法
maybe usefull (with connected signal QCustomplot::MouseMove
)
也许有用(连接信号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;
}
}
#1
9
Reimplement QCustomPlot::mouseMoveEvent
or connect to QCustomPlot::mouseMove
.
重新实现QCustomPlot :: mouseMoveEvent或连接到QCustomPlot :: mouseMove。
Then use axes' coordToPixel
to translate (cursor) pixel coords to plot coords and search nearest points in your QCPDataMap
with QMap::lowerBound(cursorX)
.
然后使用axis的coordToPixel转换(光标)像素坐标以绘制坐标并使用QMap :: lowerBound(cursorX)搜索QCPDataMap中的最近点。
#2
7
You can easily just connect a slot to the mouseMove
signal that QCustomPlot
emits. You can then use QCPAxis::pixelToCoord
to find the coordinate :
您可以轻松地将插槽连接到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));
}
#3
2
when You use datetime format (including more point per second) of X axis, then pixel to coord fails. If you want to display coordinates between points, then this is the fastest way
当您使用X轴的日期时间格式(包括每秒更多点数)时,像素到坐标将失败。如果要显示点之间的坐标,那么这是最快的方法
maybe usefull (with connected signal QCustomplot::MouseMove
)
也许有用(连接信号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;
}
}