ok, let's say that i have this QCustomPlot widget, how do i get the cordinates of the mouse cursesr when it hovers over the plot
ok, let's say that i have this QCustomPlot widget, how do i get the cordinates of the mouse cursesr when it hovers over the plot
one easy way is to install eventFilter and catch mouseEvents:
re-implement this method in your dialog or widget that contains QCP:
bool Dialog::eventFilter(QObject *target, QEvent *event) { if(target == ui->plot && event->type() == QEvent::MouseMove) { QMouseEvent *_mouseEvent = static_cast<QMouseEvent*>(event); /* Here you have mouseEvent object so you have x,y coordinate of MouseEvent. Now if you want to convert x,y coordiante of mouse to plot coordinates you can easily use QCPAxis::pixelToCoord() method. */ } return false; }
And ofcourse don't forget to install event filter in constructor:
ui->plot->installEventFilter(this);
A bit simpler might be to just connect to the mouseMove
signal that QCustomPlot emits. And in there, you do as mostafa said, i.e. use QCPAxis::pixelToCoord
to find the coordinate.
You can eaily just connect a slot to the "mouseMove" signal that QCustomPlot emits. You can then use QCPAxis::pixelToCoord to find the coordinate :
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)); }
Hi Nejt,
i did the same thing
what i did is like following, but i didn't see the xcoordinate value when i clickedon graph.
could you please suggest me where i have gone wrong
void Fos_Gui::clickedongraph(QMouseEvent *event)
{
int x = WaveWidget->xAxis->pixelToCoord(event->pos().x());
setToolTip(QString("%1").arg(x));
}
and coonected the signal like following
connect(WaveWidget,SIGNAL(mousPress(QMouseEvent*)),this,SLOT(clickedongraph(QMouseEvent*))
Thanks in advance.
Hi,
by looking at the documentation you can see that the method pixelToCoord works with double and not int. This is important if your axis range is lower than 1.
Remplacing
int x = this->xAxis->pixelToCoord(event->pos().x()); int y = this->yAxis->pixelToCoord(event->pos().y());
by
double x = this->xAxis->pixelToCoord(event->pos().x()); double y = this->yAxis->pixelToCoord(event->pos().y());
solved my problem.
Can you write the complete code please?