Solved ChartView keyboard events
-
Hi,
I want to implement zooming/scrolling functionality on my use of
ChartView
and would like to provide the option of keyboard control. How to I detect a keyboard event on aChartView
?I tried the probably naive approach of adding a
Keys.onPressed
handler to theChartView
but it does not get triggered. Unlike anItem
,ChartView
has nofocus
property, so I tried overlaying a transparent rectangle on theChartView
, settingfocus: true
on it and moving the keyboard event handler to the rectangle instead, but still no luck. Frustratingly the documentation forChartView.scrollDown
(for example) says "This is a convenience method suitable for key navigation, for example", which suggests that it should be possible.Thanks in advance for any tips.
-
I use separate input controls like RangeSlider QML Type for my axis config's.
I also have a wrapper MouseArea that I use for Key inputs to interact with that, not my chart.
I manually give and take focus according to the mouse, so keys don't interact after you've left the chart (MouseArea ) region.:MouseArea { id: itemMouseArea ; anchors.fill: parent; propagateComposedEvents: true hoverEnabled: true ; onEntered: { forceActiveFocus() } onExited: { if (configVisible === false) { itemMouseArea.focus = false; } } Keys.onPressed: { if (event.key === Qt.Key_Left) { row.updateShared(++slider.value); } if (event.key === Qt.Key_Right) { if ( slider.value > 1){ row.updateShared(--slider.value); } } event.accepted = true; } }
-
I use separate input controls like RangeSlider QML Type for my axis config's.
I also have a wrapper MouseArea that I use for Key inputs to interact with that, not my chart.
I manually give and take focus according to the mouse, so keys don't interact after you've left the chart (MouseArea ) region.:MouseArea { id: itemMouseArea ; anchors.fill: parent; propagateComposedEvents: true hoverEnabled: true ; onEntered: { forceActiveFocus() } onExited: { if (configVisible === false) { itemMouseArea.focus = false; } } Keys.onPressed: { if (event.key === Qt.Key_Left) { row.updateShared(++slider.value); } if (event.key === Qt.Key_Right) { if ( slider.value > 1){ row.updateShared(--slider.value); } } event.accepted = true; } }
-
@6thC Thank you!
I already had a
MouseArea
on the plot area of myChartView
but I hadn't thought to try to handle key events there. I think the idea of using the mouse enter and exit handlers to manually manage the focus - and in particular theforceActiveFocus
call - were what I was missing and what I needed.