Hello,

I want to edit some object in a form that have an ok and a cancel button. I have a QObject with property exposed to the QML (name, surname, ...) and this QObject has a reference to a Person object.
What I have at first is something like that :

Qt Code:
  1. Q_PROPERTY (QString name READ getName WRITE setName NOTIFY nameChanged)
  2.  
  3. QString PersonViewModel::getName () const
  4. {
  5. return person->getName();
  6. }
  7.  
  8. void PersonViewModel::setName (QString i_value)
  9. {
  10. person->set_name(i_value);
  11. emit nameChanged(i_value);
  12. }
To copy to clipboard, switch view to plain text mode 

The issue is that I want to be able to rollback all the modifications made on the form if I click on the cancel button and I want to call the person->setName() only after the user clicks on the ok button.
I thought of overriding the setProperty method to store all the modification in a map and return the value stored in the map like that

Qt Code:
  1. QString PersonViewModel::getName () const
  2. {
  3. if(map.contains("name")
  4. {
  5. return map.value("name");
  6. }
  7. else
  8. {
  9. return person->getName();
  10. }
  11. }
  12.  
  13. void PersonViewModel::setName (QString i_value)
  14. {
  15. map.insert("name", i_value);
  16. }
  17.  
  18. void PersonViewModel::commitChanges()
  19. {
  20. if(map.contains("name")
  21. {
  22. person->set_name(map.value("name");
  23. emit nameChanged(i_value);
  24. }
  25. map.clear();
  26. }
To copy to clipboard, switch view to plain text mode 

Is it ok or is there a more desirable way of doing it with QT ?

Thanks.