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. Proper way to binding QML Controls and C++ model
QtWS25 Last Chance

Proper way to binding QML Controls and C++ model

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qml to c++bindingsc++ to qml
2 Posts 2 Posters 463 Views
  • 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.
  • F Offline
    F Offline
    fromis_9
    wrote on last edited by fromis_9
    #1

    Hi. I wonder what is the best way binding QML control type and C++ model.

    // My C++ model
    class Backend : public QObject {
    Q_OBJECT
      Q_PROPERTY(bool myCheck READ myCheck WRITE SetMyCheck NOTIFY myCheckChanged)
    // ...
    }
    
    // QML binding
    CheckBox {
      id: qmlCheckBox
      checked: backend.myCheck
      onCheckedChanged: backend.myCheck = checked
    }
    

    Like above, when C++ model(@myCheck) is changed in C++ code, then it should be notified to QML so that @qmlCheckBox is also changed.
    Changing in QML also should change C++ model. (vice versa)
    But there was a binding loop detected warnings.
    I KNOW WHY THERE IS BINDING LOOP. I wonder how to do what I want without binding loop.
    Thanks.

    GrecKoG 1 Reply Last reply
    0
    • F fromis_9

      Hi. I wonder what is the best way binding QML control type and C++ model.

      // My C++ model
      class Backend : public QObject {
      Q_OBJECT
        Q_PROPERTY(bool myCheck READ myCheck WRITE SetMyCheck NOTIFY myCheckChanged)
      // ...
      }
      
      // QML binding
      CheckBox {
        id: qmlCheckBox
        checked: backend.myCheck
        onCheckedChanged: backend.myCheck = checked
      }
      

      Like above, when C++ model(@myCheck) is changed in C++ code, then it should be notified to QML so that @qmlCheckBox is also changed.
      Changing in QML also should change C++ model. (vice versa)
      But there was a binding loop detected warnings.
      I KNOW WHY THERE IS BINDING LOOP. I wonder how to do what I want without binding loop.
      Thanks.

      GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      @fromis_9 Controls usually provide a signal to indicate that a change has come from a user interaction signal, you should use this one instead of the "raw" <property>Changed signal that will fire both on backend and frontend changes.

      For CheckBox it is the toggled signal, for ComboBox activated, for TextField textEdited, ...

      So in your case it would be:

      CheckBox {
        id: qmlCheckBox
        checked: backend.myCheck
        onToggled: backend.myCheck = checked
      }
      

      Make sure to also not emit the notify signal in the setter if the property doesn't actually changed, generally done with an if at the start:

      void Backend::SetMyCheck(bool myCheck)
      {
          if (myCheck == m_myCheck) 
              return;
          m_myCheck = myCheck;
          emit myCheckChanged();
      }
      
      1 Reply Last reply
      1
      • F fromis_9 has marked this topic as solved on

      • Login

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