Problema QML Keys.onEnterPressed
-
12-12-2019 - |
Pregunta
Tengo un proyecto QtQuick para escritorio.Es muy simple:
// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1
Rectangle {
width: 360
height: 360
Grid
{
id: xGrid
width: parent.width
height: parent.height
columns: 2
spacing: 1
Rectangle
{
height: parent.height
width: 10
color: "#ff0000"
Text {
id: xText
text: qsTr("t\na\ns")
}
}
TextEdit
{
id: xTextEdit
height: parent.height
width: 350
Keys.onEnterPressed: {
console.log(event.key)
xText.text = (qsTr("A"))
}
}
}
}
Mi código no se ejecuta como quiero.El Keys.onEnterPressed
Parece que nunca será capturado, así que lo intento. Keys.onPressed
Funciona pero no estoy seguro de por qué cuando presiono Ingresar, el even.key
devuelve 16777220.
¿Alguien tiene este problema?¿Cómo puedo resolverlo?
¡Gracias por tu respuesta!
Solución
Tengo el mismo problema con un TextInput
artículo.Lo intenté
onPressed
onEnterPressed
onReturnPressed
Sólo el último funcionó (onReturnPressed
).Supongo que la implementación subyacente de la TextInput
captura la tecla 'Intro' para que no sea procesada por el onPressed
señal de forma regular.
Por cierto:El código clave es correcto.Es una abstracción de los códigos clave específicos de la plataforma.
Otros consejos
TextArea {
id: messageField
Layout.fillWidth: true
placeholderText: qsTr("Message")
wrapMode: TextArea.Wrap
inputMethodHints: Qt.ImhNoPredictiveText
function _onEnterPressed(event)
{
if ((event.modifiers & Qt.ControlModifier))
{
sendMessage()
}
else
{
event.accepted = false;
}
}
Keys.onReturnPressed: { _onEnterPressed(event) }
Keys.onEnterPressed: { _onEnterPressed(event) }
}
A better way to handle users entering a text value is to use TextInput.onAccepted
Here's an example:
TextInput {
onAccepted: processText()
}
When the user presses Enter, the processText() method will be called. This approach is simpler and should improve cross-platform portability.
I'd say use onReturnPressed as well. Otherwise you can also check the key value in onPressed() and react there. onReturn/EnterPressed are just convenience functions.
Potentially relevant context taken from the docs:
[...] the order of key event processing is:
- Items specified in forwardTo
- specific key handlers, e.g. onReturnPressed
- onPressed, onReleased handlers
- Item specific key handling, e.g. TextInput key handling
- parent item