SpinBox does not display the new value that has changed in the c++ model,
-
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() }
-
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() }
Your setData() function is missing the dataChanged() signal as described in the documentation.
-
Your setData() function is missing the dataChanged() signal as described in the documentation.
@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
-
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 } }
-
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