Results 1 to 8 of 8

Thread: QAbstractItemModel with QUndoStack

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2006
    Posts
    35
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QAbstractItemModel with QUndoStack

    I have a custom QAbstractTableModel descendant that I want to extend with a QUndoStack to allow undo/redo of changes to the model.

    How would I do this nicely? I have two ideas but I am unhappy with both.

    I created a simple QUndoCommand descendant:
    Qt Code:
    1. class SetDataCommand: public QUndoCommand
    2. {
    3. public:
    4. SetDataCommand(const QPersistentModelIndex & index, const QVariant & oldValue, const QVariant & newValue)
    5. : m_index(index),
    6. m_oldValue(oldValue),
    7. m_newValue(newValue)
    8. {}
    9.  
    10. void undo();
    11. void redo();
    12. private:
    13. const QPersistentModelIndex m_index; ///< Holds the model index.
    14. const QVariant m_oldValue; ///< Holds the old value.
    15. const QVariant m_newValue; ///< Holds the new value.
    16. };
    17.  
    18. void SetDataCommand::undo()
    19. {
    20. if (m_index.isValid())
    21. const_cast<QAbstractItemModel *>(m_index.model())->setData(m_index, m_oldValue);
    22. }
    23.  
    24. void SetDataCommand::redo()
    25. {
    26. if (m_index.isValid())
    27. const_cast<QAbstractItemModel *>(m_index.model())->setData(m_index, m_newValue);
    28. }
    To copy to clipboard, switch view to plain text mode 

    Idea 1:
    The setData(...) method changes the model data and adds the undo command to the undo stack. Problem: Pushing the undo command to the stack will execute the redo() method of the command.
    Workaround: The redo command has a boolean guard that will lead the first call to redo() do nothing. Both undo() and redo() would use a user role to make changes to the model to prevent the creation of undo commands when using undo/redo.

    Idea 2:
    The setData() method creates the undo command which will then lead to the call to redo() that will call some protected? custom method that will modify the model. The problem is that I would not know in the setData() method, if the undo command succeded with redo() and would return true without knowing.
    Workaround: I would have to do all checks before creating the undo command to be certain that the undo command will not fail to modify the data.

    Thanks in advance for comments and ideas,
    -Jens
    Last edited by No-Nonsense; 20th February 2007 at 13:00.

Similar Threads

  1. Help needed with QAbstractItemModel
    By Harvey West in forum Qt Programming
    Replies: 11
    Last Post: 27th November 2006, 12:06
  2. QAbstractItemModel for dummies
    By morty in forum Qt Programming
    Replies: 1
    Last Post: 10th October 2006, 15:25
  3. Subclassing QAbstractItemModel
    By r366y in forum Qt Programming
    Replies: 2
    Last Post: 2nd May 2006, 08:46
  4. [QT4] QTreeView, QAbstractItemModel and sorting
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 24th March 2006, 20:16
  5. Replies: 8
    Last Post: 7th March 2006, 13:40

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
  •  
Qt is a trademark of The Qt Company.