Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to bind TextInput.text to QString property in two ways?
Forum Updated to NodeBB v4.3 + New Features

How to bind TextInput.text to QString property in two ways?

Scheduled Pinned Locked Moved QML and Qt Quick
14 Posts 4 Posters 20.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? This user is from outside of this forum
    ? This user is from outside of this forum
    Guest
    wrote on last edited by
    #2

    take a look at this example on forum nokia: "http://wiki.forum.nokia.com/index.php/CS001625_-Connecting_Qt_signal_to_QML_function":http://wiki.forum.nokia.com/index.php/CS001625-_Connecting_Qt_signal_to_QML_function

    1 Reply Last reply
    0
    • A Offline
      A Offline
      axeljaeger
      wrote on last edited by
      #3

      Thank you but this does not help because it is just an alternate way to do the part that already works. I want the C++ lineedit to be updated from the QML one. And I actually want to do it in a declarative way and not an imperative way.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mbrasser
        wrote on last edited by
        #4

        Hi,

        QML currently only supports "one-way" bindings. You can use the "Binding":http://doc.qt.nokia.com/4.7/qml-binding.html element if you want to bind lineEdit.text to the text of the TextInput as well.

        @
        import Qt 4.7
        Rectangle {
        width: 240
        height: 320

        TextInput {
            id: input
            text: lineEdit.text
            anchors.fill: parent
        }
        Binding {
            target: lineEdit
            property: "text"
            value: input.text
        }
        

        }
        @

        Regards,
        Michael

        1 Reply Last reply
        0
        • A Offline
          A Offline
          axeljaeger
          wrote on last edited by
          #5

          Thank you. This is exactly the information I was looking for.

          1 Reply Last reply
          0
          • N Offline
            N Offline
            njeisecke
            wrote on last edited by
            #6

            Hi,

            Doing this will generate a console warning:

            QML TextInput: Binding loop detected for property "text"

            It works nevertheless but I don't like warnings.

            Any ideas?

            Regards

            Nils

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #7

              File a bugreport :-)

              1 Reply Last reply
              0
              • N Offline
                N Offline
                njeisecke
                wrote on last edited by
                #8

                Well, it actually is a binding loop. However it does not do any harm as long as the setter on the C++ side is implemented correctly.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #9

                  I meant it more as: file a bug report with a suggestion to make two way bindings possible :-)

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mbrasser
                    wrote on last edited by
                    #10

                    [quote author="njeisecke" date="1305030760"]Hi,

                    Doing this will generate a console warning:

                    QML TextInput: Binding loop detected for property "text"

                    It works nevertheless but I don't like warnings.

                    Any ideas?
                    [/quote]

                    Hi Nils,

                    Is it the exact snippet above (with a QLineEdit in C++) you are testing or something else?

                    Typically this warning is avoided via the C++ implementation of the setter, e.g. setText() will be implemented to only emit the NOTIFY signal if the property actually changes. I thought in this case both TextInput and QLineEdit would have done this, though (so there might be something else at play here that I've missed).

                    Regards,
                    Michael

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #11

                      Are you suggesting that Qt Quick is at runtime able to analyze if the (C++) objects block emitting a changed signal if you set the same value on a property? And that they only should give the Binding Loop warning if they don't block the signal? That would be pretty cool! But I think that is unlikely. I think it is a warning because it can not be detected if the signal is emitted. If it could, it would (should) be an error instead.

                      1 Reply Last reply
                      0
                      • N Offline
                        N Offline
                        njeisecke
                        wrote on last edited by
                        #12

                        Hi Michael,

                        my setter was on the c++ side and indeed wrongly implemented (stupid me).

                        With an implementation like this everything works as expected:

                        @
                        void MyObject::setValue(const QString &value)
                        {
                        // this avoids the (absolutely correct) warning
                        if (m_value == value)
                        return;

                        m_value = value;
                        emit valueChanged():
                        }
                        @

                        So actually there seems to be some runtime analysis. Cool ;-)

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          andre
                          wrote on last edited by
                          #13

                          Does implementing it like this fix the warning? That would be awesome!
                          If so, then perhaps the warning should be an error instead?

                          1 Reply Last reply
                          0
                          • N Offline
                            N Offline
                            njeisecke
                            wrote on last edited by
                            #14

                            Yes it does, I've just commented out the check and the warning appears.

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved