qwtplot and mouse tracking
I have an application with a qwtplot, grid and qwtcurve or histogram_item.h (I think this is irrelevant). I want to show the user the axis values in the plot that correspond to where the cursor is. So I did it like this:
First I declare an eventfilter from qwtplot to a class that I have, and I activate mousetracking:
m_plot->setMouseTracking(true);
m_plot->canvas()->setMouseTracking(true);
m_plot->installEventFilter(this);
in the eventFilter I do this (of course after detecting mouse move event):
statusbar->showMessage( QString("X: ") +
QString::number((int) (0.5 + m_plot->invTransform(QwtPlot::xBottom, mouseEvent->pos().x()))) +
QString(" Y: ") +
QString::number((int) (0.5 + m_plot->invTransform(QwtPlot::yLeft, mouseEvent->pos().y()))));
so basically, I receive mouse position, and I use invTransform method to convert it from pixel position to the values in the plot.
The axis are defined like this:
m_plot->setAxisScale(QwtPlot::yLeft, 0.0, max);
m_plot->setAxisScale(QwtPlot::xBottom, 0.0, numValues);
And now my problem. I have many plots (histograms, curves, etc.) and in all of them, I have an error of measure in the X value, this error depends on the position inside the parent (widget or frame) and/or size of the qwtplot. I don't know if the error is already in the pixels or if it is after invTransform. I can correct the difference if I don't allow to resize the plot, but I want to know where this error comes from.
If you need more code or information, please ask for it, and thanks for your help.
Re: qwtplot and mouse tracking
You need to filter the mouse events from the plot canvas not from the plot widget itsself.
Nevertheless have a look at QwtPlotPicker before you continue.
Uwe
Re: qwtplot and mouse tracking
Thanks for your answer. I've been busy lately so I haven't tried it. I'll post here if it worked when I have time to work on it.
Re: qwtplot and mouse tracking
It worked filtering from canvas(). I won't use qwtplotpicker as the current code works, but I will use it the next time. Thanks again.
Re: qwtplot and mouse tracking
Is it possible to use a QwtPlotPicker to show something in the statusbar? I have a Spectrogram and want to show the z-value to the current coordinates (and other things, but that is not so important at the moment). I tried to use "moved"-signal, but that works only if the mouse is clicked (as in "bode"-example).
How can I change that?
Re: qwtplot and mouse tracking
got it :) I had to write my own subclass of QwtPlotPicker and overriding the "trackerText" method:
Code:
{
Q_OBJECT
public:
MyPicker
(int xAxis,
int yAxis,
int selectionFlags, RubberBand rubberBand, DisplayMode trackerMode,
QwtPlotCanvas* canvas
);
signals:
void mouseMoved(const QPoint& pos) const;
protected:
virtual QwtText trackerText
(const QwtDoublePoint
& pos
) const;
};
Code:
MyPicker
::MyPicker(int xAxis,
int yAxis,
int selectionFlags, RubberBand rubberBand, DisplayMode trackerMode,
QwtPlotCanvas* canvas
) : QwtPlotPicker(xAxis, yAxis, selectionFlags, rubberBand, trackerMode, canvas
) {}
QwtText MyPicker
::trackerText (const QwtDoublePoint
& pos
) const {
const QPoint point
= pos.
toPoint();
emit mouseMoved(point);
}
now I can react to the "mouseMoved"-signal :)
Re: qwtplot and mouse tracking
I'm not sure if this is related to QwtPlotPicker or general to Qt...
as written above, I want to react to the mouseMoved-signal. Currently, I write text into the statusBar. But I want to do this only as long as the mouse is on my plot. When the user moves the mouse outside the canvas, I don't want to do anything. Is there a possibility to check if the cursor is on the canvas? Or the get a signal when the cursor has left the canvas?