Manage key events Ctrl + Tab and Ctrl + Shift + Tab

I want to handle two key events Ctrl + Tab and Ctrl + Shift + Tab in order to switch between tabs in my application (forward and backward, respectively). However, this does not seem to work properly.

This is my current code (minimal example):

 import QtQuick 1.1 Item { width: 100 height: 100 focus: true Keys.onPressed: { if(event.modifiers & Qt.ControlModifier) { if(event.key === Qt.Key_Tab) { if(event.modifiers & Qt.ShiftModifier) console.log('backward') else console.log('forward') } } } } 

I ran this piece of code using qmlviewer (Qt version 4.8.2)

Exit when you press Ctrl + Tab :

 forward forward 

Exit when you press Ctrl + Shift + Tab :

no one

So, I see two errors: the first key sequence is processed twice, and the other is not at all.

  • EDIT: The reason why the other is not processed at all is solved, see comments.

Why is this happening and how can I solve it?

Note. I already use Qt desktop components in my application, so keep this in mind if you know a solution that requires this module.

+7
source share
1 answer

You must accept the event, otherwise the event will be distributed to parents until it is accepted. The following code worked for me.

 Item { width: 100 height: 100 focus: true Keys.onPressed: { if(event.modifiers && Qt.ControlModifier) { if(event.key === Qt.Key_Tab) { console.log('forward') event.accepted = true; } else if(event.key === Qt.Key_Backtab) { console.log('backward') event.accepted = true; } } } } 

Edit:. This behavior allows parents to handle events that the child could not, for things like keyboard shortcuts.

Hope this helps!

+7
source

All Articles