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. SpinBox does not display the new value that has changed in the c++ model,

SpinBox does not display the new value that has changed in the c++ model,

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 2 Posters 615 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.
  • I Offline
    I Offline
    Idodoqdo
    wrote on last edited by
    #1

    Hello everyone. I have a SpinBox that displays the value of the model that is in the QAbstractListModel. The switch is working correctly

    CameraSettingValue.qml

    import QtQuick 2.15
    import QtQuick.Layouts 1.15
    import QtQuick.Controls 2.15
    
    Item {
        id:cell
    
        RowLayout {
            spacing: 0
            anchors.fill: parent
            Rectangle {
                border.color: "black"
                color: "black"
                Layout.preferredWidth: parent.width * 0.2
                Layout.fillHeight: true
                Switch {
                    checked: switched
                    anchors.centerIn: parent
                    visible: isHaveSwitch
                    Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
                    onClicked: {
                        switched = !switched
                    }
    
                }
            }
            Rectangle {
                color: "black"
                Layout.fillWidth:  true
                Layout.fillHeight: true
                Text {
                    font.bold: true
                    color: "white"
                    text: name
                    anchors.left: parent.left
                    anchors.verticalCenter: parent.verticalCenter
    
                }
            }
            SpinBox {
               id: spinbox
               Layout.preferredWidth: parent.width * 0.3
    
               from: 0
               value: model.value
               to: 100
    
               validator: DoubleValidator {
                   bottom: Math.min(spinbox.from, spinbox.to)
                   top:  Math.max(spinbox.from, spinbox.to)
               }
    
               onValueChanged: {
                   model.value = value
                   print(model.value)
               }
            }
        }
    }
    

    cma_cfg_set_model.cc

    ameraConfigurationSettingsModel::CameraConfigurationSettingsModel(QObject * parent)
        :QAbstractListModel(parent)
    {
        roleNames_[nameRole] = "name";
        roleNames_[valueRole] = "value";
        roleNames_[switchedRole] = "switched";
        roleNames_[isHaveSwitch] = "isHaveSwitch";
    }
    
    int CameraConfigurationSettingsModel::rowCount(const QModelIndex &parent) const
    {
        Q_UNUSED(parent)
    
        return settings_.count();
    }
    
    QVariant CameraConfigurationSettingsModel::data(const QModelIndex &index, int role) const
    {
        int row = index.row();
    
        if (row < 0 || row >= settings_.count()) {
            return QVariant();
        }
    
        const CameraConfigurationSetting& setting = settings_[row];
    
        switch (role) {
        case nameRole:
            return setting.GetName();
        case valueRole:
            return setting.GetValue();
        case switchedRole:
            return setting.IsSwitched();
        case isHaveSwitch:
            return setting.IsHaveSwitch();
        }
    
        return QVariant();
    
    }
    
    bool CameraConfigurationSettingsModel::setData(const QModelIndex &index, const QVariant &value, int role)
    {
        int row = index.row();
    
        if (row < 0 || row >= settings_.count()) {
            return false;
        }
    
        CameraConfigurationSetting& setting = settings_[row];
    
        switch (role) {
            case valueRole:
                setting.SetValue(value.toDouble());
                return true;
            case switchedRole:
                setting.Switch();
                return true;
        }
    
        return false;
    }
    
    void CameraConfigurationSettingsModel::addSetting(CameraConfigurationSetting setting)
    {
        settings_.append(setting);
    }
    
    void CameraConfigurationSettingsModel::calibrate()
    {
        for (auto &setting: settings_) {
            setting.SetValue(0);
            if (setting.IsHaveSwitch() && setting.IsSwitched()) {
                setting.Switch();
            }
        }
    
        emit dataChanged(index(0, 0), index((count() - 1), 0));
    }
    
    int CameraConfigurationSettingsModel::count() const
    {
        return settings_.count();
    }
    
    QHash<int, QByteArray> CameraConfigurationSettingsModel::roleNames() const
    {
        return roleNames_;
    }
    

    I made the calibrate invoke method. I call it when I press the qml button. The call is being made

                onButtonClicked: {
                    SettingsLeftSideModel.calibrate()
                }
    
    Christian EhrlicherC 1 Reply Last reply
    0
    • I Idodoqdo

      Hello everyone. I have a SpinBox that displays the value of the model that is in the QAbstractListModel. The switch is working correctly

      CameraSettingValue.qml

      import QtQuick 2.15
      import QtQuick.Layouts 1.15
      import QtQuick.Controls 2.15
      
      Item {
          id:cell
      
          RowLayout {
              spacing: 0
              anchors.fill: parent
              Rectangle {
                  border.color: "black"
                  color: "black"
                  Layout.preferredWidth: parent.width * 0.2
                  Layout.fillHeight: true
                  Switch {
                      checked: switched
                      anchors.centerIn: parent
                      visible: isHaveSwitch
                      Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
                      onClicked: {
                          switched = !switched
                      }
      
                  }
              }
              Rectangle {
                  color: "black"
                  Layout.fillWidth:  true
                  Layout.fillHeight: true
                  Text {
                      font.bold: true
                      color: "white"
                      text: name
                      anchors.left: parent.left
                      anchors.verticalCenter: parent.verticalCenter
      
                  }
              }
              SpinBox {
                 id: spinbox
                 Layout.preferredWidth: parent.width * 0.3
      
                 from: 0
                 value: model.value
                 to: 100
      
                 validator: DoubleValidator {
                     bottom: Math.min(spinbox.from, spinbox.to)
                     top:  Math.max(spinbox.from, spinbox.to)
                 }
      
                 onValueChanged: {
                     model.value = value
                     print(model.value)
                 }
              }
          }
      }
      

      cma_cfg_set_model.cc

      ameraConfigurationSettingsModel::CameraConfigurationSettingsModel(QObject * parent)
          :QAbstractListModel(parent)
      {
          roleNames_[nameRole] = "name";
          roleNames_[valueRole] = "value";
          roleNames_[switchedRole] = "switched";
          roleNames_[isHaveSwitch] = "isHaveSwitch";
      }
      
      int CameraConfigurationSettingsModel::rowCount(const QModelIndex &parent) const
      {
          Q_UNUSED(parent)
      
          return settings_.count();
      }
      
      QVariant CameraConfigurationSettingsModel::data(const QModelIndex &index, int role) const
      {
          int row = index.row();
      
          if (row < 0 || row >= settings_.count()) {
              return QVariant();
          }
      
          const CameraConfigurationSetting& setting = settings_[row];
      
          switch (role) {
          case nameRole:
              return setting.GetName();
          case valueRole:
              return setting.GetValue();
          case switchedRole:
              return setting.IsSwitched();
          case isHaveSwitch:
              return setting.IsHaveSwitch();
          }
      
          return QVariant();
      
      }
      
      bool CameraConfigurationSettingsModel::setData(const QModelIndex &index, const QVariant &value, int role)
      {
          int row = index.row();
      
          if (row < 0 || row >= settings_.count()) {
              return false;
          }
      
          CameraConfigurationSetting& setting = settings_[row];
      
          switch (role) {
              case valueRole:
                  setting.SetValue(value.toDouble());
                  return true;
              case switchedRole:
                  setting.Switch();
                  return true;
          }
      
          return false;
      }
      
      void CameraConfigurationSettingsModel::addSetting(CameraConfigurationSetting setting)
      {
          settings_.append(setting);
      }
      
      void CameraConfigurationSettingsModel::calibrate()
      {
          for (auto &setting: settings_) {
              setting.SetValue(0);
              if (setting.IsHaveSwitch() && setting.IsSwitched()) {
                  setting.Switch();
              }
          }
      
          emit dataChanged(index(0, 0), index((count() - 1), 0));
      }
      
      int CameraConfigurationSettingsModel::count() const
      {
          return settings_.count();
      }
      
      QHash<int, QByteArray> CameraConfigurationSettingsModel::roleNames() const
      {
          return roleNames_;
      }
      

      I made the calibrate invoke method. I call it when I press the qml button. The call is being made

                  onButtonClicked: {
                      SettingsLeftSideModel.calibrate()
                  }
      
      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Your setData() function is missing the dataChanged() signal as described in the documentation.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      I 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        Your setData() function is missing the dataChanged() signal as described in the documentation.

        I Offline
        I Offline
        Idodoqdo
        wrote on last edited by
        #3

        @Christian-Ehrlicher
        then why does the value of the Switch change?I made the following code:

        bool CameraConfigurationSettingsModel::setData(const QModelIndex &index, const QVariant &value, int role)
        {
            int row = index.row();
        
            if (row < 0 || row >= settings_.count()) {
                return false;
            }
        
            CameraConfigurationSetting& setting = settings_[row];
        
            switch (role) {
                case valueRole:
                    setting.SetValue(value.toDouble());
                    emit dataChanged(index, index);
                    return true;
                case switchedRole:
                    setting.Switch();
                    emit dataChanged(index, index);
                    return true;
            }
        
            return false;
        }
        

        It didn't help

        1 Reply Last reply
        0
        • I Offline
          I Offline
          Idodoqdo
          wrote on last edited by
          #4

          I found out that there really is 0 in the data, qml displays the old value

                  SpinBox {
                     id: spinbox
                     Layout.preferredWidth: parent.width * 0.3
          
                     from: 0
                     value: model.value
                     to: 100
          
                     validator: DoubleValidator {
                         bottom: Math.min(spinbox.from, spinbox.to)
                         top:  Math.max(spinbox.from, spinbox.to)
                     }
          
                     onValueChanged: {
                         print(model.value) // print 0 after calibrate
                         model.value = value
                     }
                  }
          
          1 Reply Last reply
          0
          • I Offline
            I Offline
            Idodoqdo
            wrote on last edited by
            #5

            I changed the default value in c++ model to 50, after I received such messages in the console:

            set value in the model c++ 50
            qrc:/CameraSettingValue.qml:40:9: QML SpinBox: Binding loop detected for property "value"
            qml: 50
            set value in the model c++ 50
            qrc:/CameraSettingValue.qml:40:9: QML SpinBox: Binding loop detected for property "value"
            qml: 50
            set value in the model c++ 50
            qrc:/CameraSettingValue.qml:40:9: QML SpinBox: Binding loop detected for property "value"
            qml: 50
            set value in the model c++ 50
            qrc:/CameraSettingValue.qml:40:9: QML SpinBox: Binding loop detected for property "value"
            

            before that, there was no

            qrc:/CameraSettingValue.qml:40:9: QML SpinBox: Binding loop detected for
            

            Now, when you click on the button, the values become 0, but after the 1st time it no longer works

            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