Chilarai
Chilarai

Reputation: 1888

Qml TextEdit Custom key event not processed properly

I wanted to add custom key events to a TextEdit. But, it seems key events are not properly processed inside TextEdit.

For example, in the code below, I am trying to handle Space key events. Although the Space keypress is recognized by the signal handler function, the output text does not contain a space. It is the same for all other key events. How do I overcome this?

import QtQuick 2.15
import QtQuick.Controls 2.15

Item{

    function processSpace(event){
        event.accepted = true
        console.log(xTextEdit.text)
    }

    TextEdit{
        id: xTextEdit
        height: parent.height
        width: parent.width
        Keys.onSpacePressed: processSpace(event)
    }
}

Upvotes: 0

Views: 392

Answers (1)

king_nak
king_nak

Reputation: 11513

You accept the event, and thus prevent the default handling.

Set event.accepted = false instead, so the event will be propagated.

Note that accepted is by default true (at least for key events), so not setting it will the accept the event

Upvotes: 1

Related Questions