Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. tabChangesFocus property in TextArea is not working
Forum Update on Monday, May 27th 2025

tabChangesFocus property in TextArea is not working

Scheduled Pinned Locked Moved QML and Qt Quick
qtquicktextareatab key
7 Posts 2 Posters 3.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    advent
    wrote on last edited by
    #1

    As explained in documentation here: http://doc.qt.io/qt-5/qml-qtquick-controls-textarea.html#tabChangesFocus-prop

    I expect the Tab key in TextArea to be accepted as input not to change the focus. But in my QML code below, whatever tabChangesFocus value assigned, it keep changing the focus into next element. I use Qt 5.4 and qmlscene to preview the QML file.

    import QtQuick 2.4
    import QtQuick.Controls 1.3
    import QtQuick.Layouts 1.1
    
    ColumnLayout {
        id: columnLayout1
    
        RowLayout {
            id: rowLayout1
            anchors.top: parent.top
            anchors.topMargin: 0
            anchors.left: parent.left
            anchors.right: parent.right
    
            TextField {
                id: textField1
                placeholderText: qsTr("Text Field")
                Layout.fillWidth: true
            }
    
            Button {
                id: btnAdd
                text: qsTr("Add")
            }
    
            Button {
                id: btnClose
                text: qsTr("Close")
            }
        }
    
        TextArea {
            id: textArea1
            Layout.fillHeight: true
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 0
            anchors.right: parent.right
            anchors.rightMargin: 0
            anchors.left: parent.left
            anchors.leftMargin: 0
            tabChangesFocus: false
        }
    }
    
    p3c0P 1 Reply Last reply
    0
    • A advent

      As explained in documentation here: http://doc.qt.io/qt-5/qml-qtquick-controls-textarea.html#tabChangesFocus-prop

      I expect the Tab key in TextArea to be accepted as input not to change the focus. But in my QML code below, whatever tabChangesFocus value assigned, it keep changing the focus into next element. I use Qt 5.4 and qmlscene to preview the QML file.

      import QtQuick 2.4
      import QtQuick.Controls 1.3
      import QtQuick.Layouts 1.1
      
      ColumnLayout {
          id: columnLayout1
      
          RowLayout {
              id: rowLayout1
              anchors.top: parent.top
              anchors.topMargin: 0
              anchors.left: parent.left
              anchors.right: parent.right
      
              TextField {
                  id: textField1
                  placeholderText: qsTr("Text Field")
                  Layout.fillWidth: true
              }
      
              Button {
                  id: btnAdd
                  text: qsTr("Add")
              }
      
              Button {
                  id: btnClose
                  text: qsTr("Close")
              }
          }
      
          TextArea {
              id: textArea1
              Layout.fillHeight: true
              anchors.bottom: parent.bottom
              anchors.bottomMargin: 0
              anchors.right: parent.right
              anchors.rightMargin: 0
              anchors.left: parent.left
              anchors.leftMargin: 0
              tabChangesFocus: false
          }
      }
      
      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi @advent,
      It is a known bug. See QTBUG-39102. Still unresolved.

      157

      A 1 Reply Last reply
      1
      • p3c0P p3c0

        Hi @advent,
        It is a known bug. See QTBUG-39102. Still unresolved.

        A Offline
        A Offline
        advent
        wrote on last edited by
        #3

        @p3c0 Hi, thanks for the information! Btw, is there any way to force Tab as input?

        p3c0P 1 Reply Last reply
        0
        • A advent

          @p3c0 Hi, thanks for the information! Btw, is there any way to force Tab as input?

          p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          @advent I think you can use "\t" for that purpose.

          textArea.insert(2,"\t") 
          //insert tab at position 2
          //textArea = id of TextArea
          

          157

          A 1 Reply Last reply
          0
          • p3c0P p3c0

            @advent I think you can use "\t" for that purpose.

            textArea.insert(2,"\t") 
            //insert tab at position 2
            //textArea = id of TextArea
            
            A Offline
            A Offline
            advent
            wrote on last edited by
            #5

            @p3c0 Sorry, that's not what I really mean. I want to force the TextArea (if it's possible) to accept Tab key as character. I know inserting Tab character in TextArea can be done with Ctrl+Tab, but I don't like that. The bug has been there for like a year. Is there any workaround?

            Btw, I tried to override the Tab key behavior in QML, but it's just doesn't work. Is this correct way to override Tab key?

            TextArea {
                id: textArea
                ...
                Keys.onPressed: {
                    if (event.key == Qt.Key_Tab) {
                        textArea.insert(/*current_position*/, "\t");
                    }
                }
            }
            
            1 Reply Last reply
            0
            • A Offline
              A Offline
              advent
              wrote on last edited by
              #6

              Finally, I can figure it out. Here is the code to override the Tab key change focus behavior:

              TextArea {
                  id: textArea
                  ...
                  Keys.onPressed: {
                      if (event.key == Qt.Key_Tab) {
                          insert(cursorPosition, "\t");
                          event.accepted = true;
                      }
                  }
              }
              
              p3c0P 1 Reply Last reply
              2
              • A advent

                Finally, I can figure it out. Here is the code to override the Tab key change focus behavior:

                TextArea {
                    id: textArea
                    ...
                    Keys.onPressed: {
                        if (event.key == Qt.Key_Tab) {
                            insert(cursorPosition, "\t");
                            event.accepted = true;
                        }
                    }
                }
                
                p3c0P Offline
                p3c0P Offline
                p3c0
                Moderators
                wrote on last edited by
                #7

                @advent Glad that you found the solution. Thanks for sharing. Please mark the post as solved so that it would be helpful to others too.

                157

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved