PDA

View Full Version : Getting sender for mouseclick with QwtPlot



lxman
21st January 2011, 18:32
Hello all,

I have run across a difficulty that I can't seem to find a direct answer to. My application has a QwtPlot instantiated. What I want to do is set things up so that when a user right-clicks anywhere on the QwtPlot window, I will receive a mouse event, and I can determine which of QwtPlot's QwtPlotItems it was that was clicked.

If the user right clicks on the lower x axis label, for instance, I would like a mouse event and then be able to determine that it was the label that was clicked on. Getting the mouse event is no problem. I have found several ways to do that. The sender, though, seems to be a little more difficult. I can retrieve a QWidget * using childAt, but then I have not been able to find a way to determine what kind of object it was that dispatched the signal. Was it the QLabel on the bottom x axis, the axis markers on the left y axis, etc. When I debug and look at the information that I receive (i.e. what information is visible in the debugger regarding the particular object), the objectName is always blank, so that doesn't help me. It also appears that all of the messages come from either the QwtPlot itself or the QwtPlotCanvas, depending on which method I use.

I've tried subclassing my QwtPlot and overriding the mousePressEvent, but thus far I seem to be unable to determine who exactly was clicked on.

Anyone have any ideas to steer me in the right direction?

d_stranz
23rd January 2011, 01:43
You should be asking this question in the Qwt subforum, not here in the main Qt forum.

To answer the question, you seem to be misunderstanding how Qwt is implemented. The labels on the axes, for example, are not QLabel widgets. They are QwtText (which is similar to a QString) which has been painted onto the QwtScaleWidget that represents the axis. Clicking on the QwtText does nothing, because it isn't a QWidget and doesn't emit signals.

Likewise, QwtPlotItem instances are not QWidgets, so they do not emit signals either.

If you click somewhere on the plot, the childAt() method is probably retrieving either the QwtPlotCanvas (the area where the plot items are displayed) or one of the QwtScaleWidget instances if you clicked on an axis. And that's it. Aside from QwtPlot itself, those are the only actual QWidget instances. Everything else is just stuff that is painted onto the canvas or scales.

If you want to implement a system that identifies what plot item has been clicked by the user, you're going to have to do it yourself. I'd suggest you first look at QwtPlotPicker, which will tell you (in data coordinates) where the click occurred on the canvas. Then, you will have to keep a list of all of your plot items and traverse it to see if the click occurred within one of their bounding rects (for example), or if you are plotting symbols as part of a curve, you'll have to iterate over the curve points and see if the pixel coordinates of the click fall within the pixel-based rect of a symbol. If you need to convert between data coordinates and pixels, you'll use the QwtScaleMaps for the appropriate axes.

This is based on my knowledge of Qwt 5.x. Uwe has a release candidate for a new 6.0 version, but I do not know if it provides any more support for plot item selection than 5.x.