Results 1 to 8 of 8

Thread: QAbstractItemModel with QUndoStack

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: QAbstractItemModel with QUndoStack

    Quote Originally Posted by No-Nonsense View Post
    The undo / redo commands would use the standard model interface or some added custom interface so that the model would emit dataChanged and make use of beginInsert... etc.
    Ok, but if you use setData() in your app that creates a QUndoCommand which calls setData again, you'll have an endless recursion. Use of custom methods is a solution here of course.

    The problem for me is how to implement this. I have a filtered table view on the model and a configurable number of widgets using a QDataWidgetMapper.
    Well, in that case it'd be hard to do it this way.

  2. The following user says thank you to wysota for this useful post:

    No-Nonsense (21st February 2007)

  3. #2
    Join Date
    Nov 2006
    Posts
    35
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    25

    Default Re: QAbstractItemModel with QUndoStack

    Quote Originally Posted by wysota View Post
    Ok, but if you use setData() in your app that creates a QUndoCommand which calls setData again, you'll have an endless recursion. Use of custom methods is a solution here of course.
    This is wy I thought of either the custom methods or to use a custom role for undo/redo. E.g. the model is modified from the outside using setData(..., Qt::EditRole) and undo/redo use setData(..., UndoRedoRole) so i would not create a undo command when accessed using the UndoRedoRole.

    But then my problem remains: Would I prevent the first call to redo (i.e. have it do nothing) to be able to return false from setData or would I return true, even if nothing was changed (because the created undo command failed changing the data in redo).

    -Jens

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

    Default Re: QAbstractItemModel with QUndoStack

    If changing the model is a cheap operation, I would ignore the situation and let the model set the data to what it was before.

    BTW. Monitoring the EditRole might not be enough - depends on the model and how versatile you want your solution to be.

  5. The following user says thank you to wysota for this useful post:

    No-Nonsense (21st February 2007)

  6. #4
    Join Date
    Sep 2007
    Posts
    29
    Thanks
    8

    Default Re: QAbstractItemModel with QUndoStack

    hi,
    following the same issue i thought it might be feasible to use a proxy model for that. this, of course, would offer reusability and reduce the complexity of the actual model greatly.

    now, 2 problems come to my mind:
    1) signal connection. when undoing, the source model's events shouldn't be added to the undo stack. a simple, but somewhat hacky approach could be to just disconnect from the source model while undoing.
    2) undoing insert/remove item events. i guess the structure of the model had to be parsed, eg. the cols/rows upwards from a removed item, to insert it back again at this very positions. seems also a little hacky...

    am i missing some points that could turn out to be problematic?

  7. #5
    Join Date
    Oct 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractItemModel with QUndoStack

    You could add to your model private section friend class SetDataCommand. Then can call protected function of model in the SetDataCommand.

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.