tabChangesFocus property in TextArea is not working



  • 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
        }
    }

  • Moderators

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



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


  • Moderators

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

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


  • @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");
            }
        }
    }
    


  • 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;
            }
        }
    }
    

  • Moderators

    @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.