Search code examples
qtqmlqt5qt-quick

Qt Quick: Is it possible to have text like object with onClicked function?


I am quite new to Qt Quick.

I would love to have an object with editable text, that can also have onClicked function.

Here is a simple example, of what I would like to do:

TextField {
     id: myTextField
     text: qsTr("enter text here")
     font.pixelSize: 12
     onClicked: {
        myText.text = "TextField edited"
     }
}

Text {
     id: myText
     font.pixelSize: 12
}

Solution

  • You have to use MouseArea:

    TextField {
        id: myTextField
        text: qsTr("enter text here")
        font.pixelSize: 12
        MouseArea{
            anchors.fill: parent
            onClicked:  myText.text = "TextField edited"
        }
    }
    
    Text {
        id: myText
        font.pixelSize: 12
    }