QML TextInput的onEditingFinished没有触发

时间:2019-06-03 19:58:01

标签: qt qml textinput

我有以下ButtonInputIcon项,其中包括TextInput。但是,我无法捕获editingFinished的{​​{1}}信号。我在做什么错了?

TextInput

我正在尝试在其.qml文件中捕获到editFinished信号。

Rectangle {
    width: 86
    height: 48
    property bool buttonActive: true
    property url iconSource: "../images/IC066UpDown.svg"
    property real iconSize: 0.60
    property alias textInputText: textInput.text

    color: buttonActive ? Style.buttonColorOn : Style.buttonColorOff
    radius: 5
    property alias inputMouse: inputMouse
    property alias textInput: textInput

    Rectangle {
        id: borderRect
        anchors.left: parent.left
        anchors.leftMargin: 8
        anchors.right: icon.left
        anchors.rightMargin: 8
        anchors.top: parent.top
        anchors.topMargin: 6
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 6
        border.color: buttonActive ? Style.buttonColorTextOn : Style.buttonColorTextOff
        color: parent.color
        border.width: 1

        TextInput {
            id: textInput
            color: buttonActive ? Style.buttonColorTextOn : Style.buttonColorTextOff
            text: "10"
            anchors.verticalCenter: parent.verticalCenter
            anchors.right: parent.right
            anchors.rightMargin: 5
            inputMask: "999"
            maximumLength: 3
            selectionColor: "green"
            font.family: stdFont.name
            font.pixelSize: 20
        }

        MouseArea {
            id: inputMouse
            anchors.fill: borderRect
        }
    }

    Image {
        id: icon
        sourceSize.height: parent.height * iconSize
        anchors.verticalCenter: parent.verticalCenter
        anchors.right: parent.right
        anchors.rightMargin: 10
        fillMode: Image.PreserveAspectCrop
        source: iconSource
        antialiasing: false
        visible: false
    }

    ColorOverlay {
        color: buttonActive ? Style.buttonColorTextOn : Style.buttonColorTextOff
        anchors.fill: icon
        source: icon
    }
}

编辑: 我已经进一步简化了代码,现在可以在最简单的应用程序中运行它。我仍然看不到应该触发ButtonInputIconForm { textInput.onFocusChanged: console.info("textinput") textInput.onEditingFinished: { console.info("Editing finished ", textInputText) } inputMouse.onClicked: { textInput.focus = true } } 时触发的信号。以下是新代码。

onEditingFinished中的代码:

ButtonInputIconForm.ui.qml

import QtQuick 2.4 import QtGraphicalEffects 1.12 Rectangle { width: 86 height: 48 property bool buttonActive: true property real iconSize: 0.60 property alias textInputText: textInput.text color: "green" property alias inputMouse: inputMouse property alias textInput: textInput Rectangle { id: borderRect anchors.left: parent.left anchors.leftMargin: 8 anchors.right: icon.left anchors.rightMargin: 8 anchors.top: parent.top anchors.topMargin: 6 anchors.bottom: parent.bottom anchors.bottomMargin: 6 border.color: "red" color: parent.color border.width: 1 TextInput { id: textInput color: "black" text: "10" anchors.verticalCenter: parent.verticalCenter anchors.right: parent.right anchors.rightMargin: 5 inputMask: "999" maximumLength: 3 selectionColor: "blue" font.pixelSize: 20 } MouseArea { id: inputMouse anchors.fill: borderRect } } Image { id: icon sourceSize.height: parent.height * iconSize anchors.verticalCenter: parent.verticalCenter anchors.right: parent.right anchors.rightMargin: 10 antialiasing: false visible: false } } 中的代码:

ButtonInputIcon.qml

import QtQuick 2.4 ButtonInputIconForm { textInput.onActiveFocusChanged: { if (activeFocus) { console.info("active focus on textinput") } else { console.info("active focus not on textinput") } } textInput.onFocusChanged: console.info("focus on textinput") inputMouse.onClicked: { textInput.focus = true } } 中的代码

main.qml

编辑2

我了解到,由于设置了import QtQuick 2.12 import QtQuick.Window 2.12 Window { visible: true width: 640 height: 480 title: qsTr("Hello World") property alias buttonIcon: buttonIcon ButtonInputIcon{ id: buttonIcon textInput.onEditingFinished: { console.info("buttonIcon.textInput.onEditingFinished", buttonIcon.textInputText) } } } inputMask: "999"仅在按回车键之前输入3位数字时触发。就是这个问题。

0 个答案:

没有答案