Reputation: 1784
In C++ we can use QAbstractItemModel::setData()
to modify the model's data. Searching the internet I only found how to read data from model to display it in a delegate. There are also some examples of adding and removing rows, but I could not find how to change the data of specific model index. Something like:
Slider {
onValueChanged: myModel.setData(0, {amount: value})
}
How can I modify data in a model from in QML?
Upvotes: 2
Views: 2567
Reputation: 155
In my projects I follow a different way to read/write data from/to QML models I simply create two .qml file one to display and the another is a helper file to do database operations, and I create a model that inherits QAbstractItemModel and add four functions to it:
MyCustomObject * at(int index); // get an item to display
void reload(); // to notify QML view on update/delete
bool insert(MyCustomObject *p_myCustomObject); // insert an item in model or database
bool update(MyCustomObject *p_myCustomObject);// update an item to model or database
bool doDelete(int myCustomObjectID);// delete an item from model or database
then I create a local object to read/write, and when displaying these data I populate the local object value from the model and when I want to save I write that object to database
add this property to your main display class
property MyCustomObject myCustomObject : MyCustomObject{} // to read/write UI value ti/from it
and here is a helper class that reads UI values and insert, update or delete to/from models
Note: this class is for one of my applications, though just read it and modify it to meet your needs
import QtQuick 2.0
import DOO.Commands.Son 1.0
import DOOTypes 1.0
QtObject {
// read ui values into local son
function readUIValues()
{
var v_son = SonFactory.createObject()
v_son.name = sonName.text
v_son.image = sonImage.picture
v_son.age = sonAge.text
v_son.entryDate = Date.fromLocaleDateString(Qt.locale(), sonEntryDate.text, "dd-MM-yyyy")
v_son.commingFrom = sonCommingFrom.text
v_son.disabilityKind.kind = sonDisabilityKind.currentIndex
v_son.caseDescription = sonCaseDescription.text
return v_son
}
// simple UI validation
function validateUIValues()
{
if(sonName.text == "") return false
if(sonImage.picture == "") return false
if(sonAge.text < 1 || sonAge.text > 100) return false
if(Date.fromLocaleDateString(Qt.locale(), sonEntryDate.text, "dd-MM-yyyy") == "Invalid Date") return false
if(sonCommingFrom.text == "") return false
if(sonDisabilityKind.text == "") return false
if(sonCaseDescription.text == "") return false
return true
}
// save or update a son into database
function save()
{
if (!validateUIValues())
{
dooNotifier.showMessage("خطأ","ليدك مدخلات غير صحيحة، يُرجى التأكد من إدخال قيم صحيحة")
return
}
var v_son = readUIValues()
if(disMode === DOO.CreateNew)
{
if(SonResultsModel.insert(v_son))
{
dooNotifier.showMessage("تم بنجاح","تم إضافة الابن بنجاح")
sonDisplay.hide()
}
else
{
dooNotifier.showMessage("فشل","فشل إضافة الابن")
DOOLogger.log(SonResultsModel.lasrErrorText())
}
}
else
{
//get the ID of the son bieng edited
v_son.sonID = son.sonID
if(SonResultsModel.update(v_son))
{
dooNotifier.showMessage("تم بنجاح","تم تحديث الابن بنجاح")
sonDisplay.hide()
}
else
{
dooNotifier.showMessage("فشل","فشل تحديث الابن")
DOOLogger.log(SonResultsModel.lasrErrorText())
}
}
v_son.destroy()
}
function doDelete(sonID)
{
if(SonResultsModel.doDelete(sonID)) {
dooNotifier.showMessage("تم بنجاح","تم حذف الابن بنجاح")
sonDisplay.hide()
}
else dooNotifier.showMessage("فشل","فشل حذف الابن")
}
}
this is the way I use to read and write to/from databases I hope it helps
Upvotes: 1