PDA

View Full Version : Beginner problem - ReferenceError: text is not defined



zodd
13th June 2019, 09:29
Hello,

I'm new in Qt-Quick and QML.

I'm learning how to use it with C++.
So I start with this tutorial and I use Q_PROPERTY(QString userName READ userName WRITE setUserName NOTIFY userNameChanged) :
https://doc.qt.io/qt-5/qtqml-cppintegration-topic.html

with the only difference that I use pageform.ui.qml files.

so on a pageforme I have a text field:

TextField {
id: textfield
text: "Text"
placeholderText: qsTr("User name")
anchors.centerIn: parent

}

and in main.qml:


Page3Form {
textfield
{
text: backend.userName
onTextChanged: backend.userName = text
}
}


if I use it like that onTextChaned doesn't works and I've got an error in the debugger console:

qrc:/main.qml:51: ReferenceError: text is not defined

But if I do this in main.qml :


Page3Form {
textfield
{
text: backend.userName
onTextChanged: backend.userName = textfield.text
}
}

It works but I have this message:

qrc:/Page3Form.ui.qml:25:5: QML TextField: Binding loop detected for property "text"

is someone has a clue?

Regards.

anda_skoa
13th June 2019, 14:28
Do you need every text change to be forwarded to the C++ object?
Or could you do with just onEditingFinished?

Cheers,
_

zodd
13th June 2019, 14:53
You're right, onEditingFinished is better for what I want to do and the warning is gone.

Thank you

PS: I had a look on the documentation and i found onEditingFinsihed but I didn't find onTextChanged, is it normal?
https://doc.qt.io/qt-5/qml-qtquick-controls-textfield.html
https://doc.qt.io/qt-5/qml-qtquick-controls2-textfield.html

anda_skoa
14th June 2019, 07:33
PS: I had a look on the documentation and i found onEditingFinsihed but I didn't find onTextChanged, is it normal?


Yes, because textChanged() is not an explicit signal.

All properties have associated signals for indicating that their value has changed.
The naming convention for these is "property name" + "Changed"

So, since the TextField has a "text" property, it also has a "textChanged" signal.

These property change notification signals are not documented to avoid bloating the documentation, i.e. only additional, element specific signals are listed.

Cheers,
_

P.S.: as a general rule of thumb also check that your property setter function emits its associated signal only when the property's value really changed.
This is to avoid unnecessary binding reevaluation

zodd
14th June 2019, 09:10
OK, I understand.

Thank your for the clarification and for your advice ;)