Results 1 to 4 of 4

Thread: Buffer form modifications before commiting them to the model

  1. #1

    Default Buffer form modifications before commiting them to the model

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Buffer form modifications before commiting them to the model

    You can go with the map, or with member variables for each property or by using two Person objects.

    Btw, your setters should check if there is actually any change before emitting the notification signal.

    And your commitChanges() method doesn't need to emit them at all because as far as the QML side is concerned there is no value change (the getter will return the same value, just from the Person object instead of the map).

    Cheers,
    _

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Buffer form modifications before commiting them to the model

    Alternatively the form can operate on a temporary object (e.g. a JavaScript object) and commit the changes to the real object only after the form is confirmed with the ok button.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Buffer form modifications before commiting them to the model

    I usually use the method of two objects. The constructor for the form takes a reference to a const object instance as an argument (or I provide a setter method with the same argument). This is copied into a second object instance owned by the form. All of the get and set operations used by the form are done using the form's instance. When the user clicks OK, the caller can retrieve the form's copy of the object through a getter method.

    Using this scheme, the form doesn't need to emit any signals and the form is in control over what happens to its copy of the object. When the caller receives the Accepted result from clicking the OK button (if the form is in a dialog), the caller knows that all of the requirements for a successful edit have been satisfied (otherwise the OK button wouldn't have been enabled).
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 1
    Last Post: 19th March 2014, 16:55
  2. Replies: 1
    Last Post: 29th August 2013, 05:41
  3. Replies: 6
    Last Post: 19th June 2012, 15:07
  4. modifications of decimal separator
    By 21did21 in forum Qwt
    Replies: 2
    Last Post: 2nd August 2011, 21:41
  5. Documenting modifications in LGPL QT
    By jupi32000 in forum Newbie
    Replies: 1
    Last Post: 25th July 2009, 11:58

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.