PDA

View Full Version : Focus handling for the QwtPlot



kecsap
18th November 2012, 12:38
Hi,

I noticed that I don't receive focus in and out events for the QwtPlot and the keyboard focus is not grabbed properly. To make it work, I have to fix two things:

- Set setFocusPolicy(Qt::StrongFocus) on all diagrams. -> the diagram grabs the keyboard focus after a mouse click
- Call setFocus() on the QwtPlot when e.g a mouse click happens -> fixes the focus in/out events

I hope it helps for others.

Csaba

Uwe
18th November 2012, 13:30
It seems you missed, that QwtPlot is a composite widget and all what needs to be done is to modify the focus policy ( QWidget::setFocusPolicy() ) for the sub widgets ( if you need something different as the default settings ).

For the focus handling Qwt widgets are not different to any other Qt widget.

Uwe

PS: with QwtPlotCanvas::setFocusIndicator() you can enable/disable a focus indicator for the canvas widget.

kecsap
18th November 2012, 18:38
It seems you missed, that QwtPlot is a composite widget and all what needs to be done is to modify the focus policy ( QWidget::setFocusPolicy() ) for the sub widgets ( if you need something different as the default settings ).

For the focus handling Qwt widgets are not different to any other Qt widget.

Uwe

PS: with QwtPlotCanvas::setFocusIndicator() you can enable/disable a focus indicator for the canvas widget.

Ok, I did not take into account this composite widget stuff. So what is my expectation:
- There is a QwtPlot widget without focus and if I click inside or outside the canvas area (e.g the axis area) with the left mouse button, I can grab a FocusIn event on the QwtPlot widget and the keyboard shortcuts work with the e.g QwtPlotZoomer.

Actual (out-of-box) outcome:
- Clicking on the canvas area, I don't receive FocusIn event for the QwtPlot widget (and I will never receive a FocusOut when clicking on other widgets) and the keyboard shortcuts do not work. I have to press on the canvas widget area to make the shortcuts work. I think this behavior can be confusing for the users.
- If I call the setFocusPolicy() on the QwtPlot, I receive FocusIn/FocusOut events for the QwtPlot widget only if I click outside the canvas area, but it does not work if I click on the canvas.

Workaround:
- What I must do to make everything work, call:

setFocus(); // set focus on QwtPlot widget -> makes focus in/out events work for QwtPlot
canvas()->setFocus(); // set focus on composite canvas -> keyboard shortcuts work even if the click happened outside the canvas area

... when I receive a mouse press/wheel or other related events on the QwtPlot widget. Also I don't know how much "Qtish" what I expect from the current situation. I do this whole thing to add gesture support for my QwtPlot widgets with my own gesture recognizers.

PS: I use Qwt 6.0 on Ubuntu Oneiric.

Uwe
19th November 2012, 07:28
Ok, I did not take into account this composite widget stuff. So what is my expectation:
- There is a QwtPlot widget without focus ( or other events ) and if I click inside or outside the canvas area (e.g the axis area) with the left mouse button, I can grab a FocusIn event on the QwtPlot widget and the keyboard shortcuts work with the e.g QwtPlotZoomer..
It works like this: when a widget doesn't accept the focus it is propagated to its parent. This is how Qt works - nothing special for Qwt.

The default setting is that canvas and legend items ( depending on the mode ) accept the focus the other widgets don't. You could modify this - f.e. by setting Qt::NoFocus as focus policy for the canvas and then the plot widget will get FocusIn/Out events when you click on the canvas too, but this will break the zoomer, that handles key events on the canvas ( modifying them is probably what you really want to do ).

Instead do "plot->setFocusProxy( plot->canvas() );" when you also want to have the focus on the canvas, when you click on the scales.

Uwe