How to make onEditingFinished available to individual TextInput elements reusing a common TextInput



  • I am new to QML & Qt. I am trying to make multiple TextInput elements which can send their own text when onEditingFinished is triggered. Following is a TextInput item that I have created in MyTextField.qml:

    MyTextField.qml

    import QtQuick 2.5
    import QtQuick.Controls 1.4
    
    Item {
    
    implicitHeight: 200
    implicitWidth: 1000
    
    property alias inputMethodHints: myTextField.inputMethodHints
    property alias text: myTextField.text
    
    Rectangle {
        anchors.fill: parent
        radius: 40
      }
    
    
    TextInput {
        id: myTextField
        objectName: "myTextField"
        anchors.fill: parent
        verticalAlignment: Text.AlignVCenter
        font.pixelSize: 300
        color: 'white'
    
        signal qmlSignal(string msg)
        onEditingFinished: qmlSignal(text)  //This works
      }
    }
    

    I am trying to use the above TextInput element in another qml file like below:

    SomeOtherPage.qml

    Column {
        anchors.top: parent.top
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.margins: theme.defaultMargin
    
        MyTextField {
            id: textfield1
            objectName: "textfield1"
            anchors.left: parent.left
            anchors.right: parent.right
            text: qsTr("some text")
    
           signal qmlSignal11(string msg)
           onEditingFinished: qmlSignal11(text)  //This doesn't work !!
        }
    
        MyTextField {
            id: textfield2
            objectName: "textfield2"
            anchors.left: parent.left
            anchors.right: parent.right
            text: qsTr("some other text")
    
           signal qmlSignal22(string msg)
           onEditingFinished: qmlSignal22(text)  //This doesn't work !!
        }
    }
    

    InMyTextField blocks, QML doesn't allow me to use onEditingFinished at all. Running the app complains Cannot assign to non-existent property "onEditingFinished"

    If I handle onEditingFinished from the parent TextInput I created, it works fine & sends the signal to my C++ class. But I am trying to use onEditingFinished in textfield1 & textfield2. Qml reports that onEditingFinished property is not available. How I can make onEditingFinished available in textfield1 & textfield2 so that I send the text of each unique textfield I create.


  • Moderators

    Hi!

    MyTextItem.qml

    import QtQuick 2.7
    import QtQuick.Controls 2.0
    
    Rectangle {
        id: myTextItem
        radius: 10
        width: 100
        height: 30
    
        signal editingFinished()
    
        TextInput {
            anchors.margins: 5
            anchors.fill: parent
            text: "Hallo"
            onEditingFinished: myTextItem.editingFinished()
        }
    }
    

    main.qml

    import QtQuick 2.7
    import QtQuick.Controls 2.0
    import QtQuick.Layouts 1.0
    
    ApplicationWindow {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        Column {
            spacing: 10
            MyTextItem {
                color: "plum"
                onEditingFinished: console.log("finished plum")
            }
            MyTextItem {
                color: "lime"
                onEditingFinished: console.log("finished lime")
            }
        }
    }
    


  • @Wieland thanks a many lot. it works


Log in to reply
 

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