Results 1 to 5 of 5

Thread: Beginner problem - ReferenceError: text is not defined

  1. #1
    Join Date
    Sep 2013
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Beginner problem - ReferenceError: text is not defined

    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:
    Qt Code:
    1. TextField {
    2. id: textfield
    3. text: "Text"
    4. placeholderText: qsTr("User name")
    5. anchors.centerIn: parent
    6.  
    7. }
    To copy to clipboard, switch view to plain text mode 

    and in main.qml:
    Qt Code:
    1. Page3Form {
    2. textfield
    3. {
    4. text: backend.userName
    5. onTextChanged: backend.userName = text
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 


    if I use it like that onTextChaned doesn't works and I've got an error in the debugger console:
    Qt Code:
    1. qrc:/main.qml:51: ReferenceError: text is not defined
    To copy to clipboard, switch view to plain text mode 

    But if I do this in main.qml :
    Qt Code:
    1. Page3Form {
    2. textfield
    3. {
    4. text: backend.userName
    5. onTextChanged: backend.userName = textfield.text
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    It works but I have this message:
    Qt Code:
    1. qrc:/Page3Form.ui.qml:25:5: QML TextField: Binding loop detected for property "text"
    To copy to clipboard, switch view to plain text mode 

    is someone has a clue?

    Regards.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Beginner problem - ReferenceError: text is not defined

    Do you need every text change to be forwarded to the C++ object?
    Or could you do with just onEditingFinished?

    Cheers,
    _

  3. #3
    Join Date
    Sep 2013
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Beginner problem - ReferenceError: text is not defined

    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-c...textfield.html
    https://doc.qt.io/qt-5/qml-qtquick-c...textfield.html

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Beginner problem - ReferenceError: text is not defined

    Quote Originally Posted by zodd View Post
    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

  5. #5
    Join Date
    Sep 2013
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Beginner problem - ReferenceError: text is not defined

    OK, I understand.

    Thank your for the clarification and for your advice

Similar Threads

  1. [Qml/c++] ReferenceError: code is not defined
    By Nio74 in forum Qt Programming
    Replies: 0
    Last Post: 16th March 2019, 08:08
  2. Replies: 2
    Last Post: 8th November 2016, 05:44
  3. Replies: 10
    Last Post: 1st August 2014, 07:50
  4. Beginner problem with connect
    By lucastonon in forum Newbie
    Replies: 13
    Last Post: 14th July 2011, 17:32
  5. Qfiledialog Problem - (Beginner)
    By kingslee in forum Qt Tools
    Replies: 3
    Last Post: 12th October 2006, 00:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.