3

I have two TextEdit boxes and one custom button widget, I wish to change focus in the following order using the tab key on my keyboard:

TextEdit1 <-> TextEdit2 <-> Button

I have specified something similar to the following for each widget in order to obtain the chain above:

KeyNavigation.tab: TextEdit2
KeyNavigation.backtab: TextEdit1

My problem is however that the tab keystroke is caught in the TextEdit, and cannot be used to navigate. How can I disable tabs in the TextEdit and instead use it for changing focus?

Jonatan
  • 2,734
  • 2
  • 22
  • 33

3 Answers3

5

I found the problem.

By default the key events are first sent to the item which is receiving the event - not to KeyNavigation. This behavior can be changed by setting

KeyNavigation.priority: KeyNavigation.BeforeItem

The complete code thus becomes

KeyNavigation.tab: TextEdit2
KeyNavigation.backtab: TextEdit1
KeyNavigation.priority: KeyNavigation.BeforeItem
Jonatan
  • 2,734
  • 2
  • 22
  • 33
3

Read about the Qml Keys element.

You can do something like this :

TextEdit
{
  width : 40
  height: 40
  text  : "junk"

  Keys.onTabPressed: 
  {
      // Write logic to transfer focus to whomsoever you want
  }
}
Amit Tomar
  • 4,800
  • 6
  • 50
  • 83
  • Thanks for your answer! I thought about this, but the KeyNavigation approach seems cleaner - do you know if there is a way in which I can use KeyNavigation instead? – Jonatan Jan 14 '13 at 21:14
  • Now I found the setting! Thank you for this alternative approach. – Jonatan Jan 14 '13 at 21:21
0

While searching for a solution to a similar problem, I came across this option in Qt Creator and seems to solve the thing. Now I can move out of the QTextEdit object with tab key, instead of inserting a tab character into the field.

I see that the topic is old and already solved but maybe this convenient simple option was made available with a more recent update to Qt, I don't know. I just came across it and I hope it helps someone searching for the same solution as me.

enter image description here

Doğukan Tunç
  • 337
  • 2
  • 16
  • This has nothing to do with OP question which is about QML/QtQuick – Massimo Callegari Dec 25 '20 at 11:14
  • 1
    true, yet it helped me :-) but Dogukan's answer belongs in https://stackoverflow.com/questions/39146509/pressing-tab-in-qtextedit-in-dialog-change-behavior – axd May 29 '21 at 01:47