Grégoire Borel
Grégoire Borel

Reputation: 2028

Create one custom QML signal for several properties

I have an Item composed of several properties:

Item
{
    id: propertiesList
    property CustomLabel phone: { text: dataFromServer.phone }
    property CustomLabel age: { text: dataFromServer.age}
}

I'd like to trigger a signal when the text of one of the properties is changed:

onTextChanged
{
    doSomething(phone/age.text)
}

I could create one signal for each property but I don't find this very clean. Is there a way to create one signal for all properties that would handle the text variable ?

EDIT:

CustomLabel.qml

import QtQuick 2.5
import QtQuick.Controls 1.4

Label
{
    signal textChanged(string updatedText)

    font.pixelSize: 22
    font.italic: true
    color: "steelblue"
}

Upvotes: 0

Views: 450

Answers (1)

drosam
drosam

Reputation: 2896

First you need a signal that is triggered when the text in the CustomLabel is changed. Then you can connect the signals to a signal in your Item. Like this

Item
 {
     id: propertiesList
     signal textChanged(string updatedText)

     property CustomLabel phone: { text: dataFromServer.phone, onTextChanged: doSomething(updatedText) }
     property CustomLabel age: { text: dataFromServer.age, onTextChanged: doSomething(updatedText) }
 }

-- UPDATE --

Check this. It says

Property Change Signal Handlers

Signal handlers for property change signal take the syntax form on<Property>Changed where <Property> is the name of the property, with the first letter capitalized. For example, although the TextInput type documentation does not document a textChanged signal, this signal is implicitly available through the fact that TextInput has a text property and so it is possible to write an onTextChanged signal handler to be called whenever this property changes:

So, as Label has also a text property, it has also the textChanged signal. I think that you have a conflict with the two signals (the original that Label has because the text property and the one you create in CustomLabel). Give another name to your textChanged signal in CustomLabel and it should work.

Upvotes: 1

Related Questions