Results 1 to 7 of 7

Thread: Refuse from removing row in Model after unsuccessful drop

  1. #1
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Refuse from removing row in Model after unsuccessful drop

    Hello!
    Win7, Qt 4.8.
    I use Model/View based on QStandardItemModel and QTreeView. Some items are not allowed to get dropped items. When I try to drop into them, they don't get the dropped item (as they should), but the source item is removed after the failed drop attempt.
    The same situation is described here. Is it really a bug?
    I tried to reimplement dropMimeData, f.e:
    Qt Code:
    1. bool TMyModel::dropMimeData(const QMimeData * apData, Qt::DropAction aAction, int aRow, int aColumn, const QModelIndex & aParent)
    2. {
    3. if (/*some condition*/)
    4. return QAbstractItemModel::dropMimeData ( apData, Qt::IgnoreAction, aRow, aColumn, aParent );
    5. return QAbstractItemModel::dropMimeData ( apData, aAction, aRow, aColumn, aParent );
    6. }
    To copy to clipboard, switch view to plain text mode 
    or so
    Qt Code:
    1. bool TMyModel::dropMimeData(const QMimeData * apData, Qt::DropAction aAction, int aRow, int aColumn, const QModelIndex & aParent)
    2. {
    3. if (/*some condition*/)
    4. return false;
    5. return QAbstractItemModel::dropMimeData ( apData, aAction, aRow, aColumn, aParent );
    6. }
    To copy to clipboard, switch view to plain text mode 
    RemoveRows is called in all cases and the dragged item disappears. Could I avoid the calling of removeRows?

    Thanks for any help.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Refuse from removing row in Model after unsuccessful drop

    Can you show the implementation of your Dragging operation? (at the source object)
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Refuse from removing row in Model after unsuccessful drop

    Thanks for reply.
    Maybe the problem is here and I should reimplement some other functions? F.e., supportedDragActions? I try to reimplement only dropMimeData. (Also 'data' and 'setData' but I think they are not important here)

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Refuse from removing row in Model after unsuccessful drop

    What to do with the dragged data on the source side has to be implemented on the soruce side - where the drag begins, and where the drop result returns.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Refuse from removing row in Model after unsuccessful drop

    It becomes a little bit clearer.
    Really 'removeRows' is called in QAbstractItemView::startDrag in such a way:
    Qt Code:
    1. if (drag->exec(supportedActions, defaultDropAction) == Qt::MoveAction)
    2. d->clearOrRemove();
    To copy to clipboard, switch view to plain text mode 
    But I can't understand how to get out of it. Should I reimplement 'startDrag' at all or is it possible to avoid calling 'clearOrRemove' by some other tricks?

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Refuse from removing row in Model after unsuccessful drop

    I think you are over complicating all of this.
    Simply call ignore on your dragEnter event when the mime type of your drag should be ignored.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Refuse from removing row in Model after unsuccessful drop

    There are no certain mime types to ignore. The necessity to ignore drop depends on the drop position. F.e., I don't want to drop items in the second column in the tree.
    Well, I did the following:
    Qt Code:
    1. TMyModel : public QStandardItemModel
    2. public:
    3. void setErrorFlag(bool);
    4. bool getErrorFlag();
    5.  
    6. private:
    7. bool ErrorFlag;
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. bool TMyModel::dropMimeData(const QMimeData * pData, Qt::DropAction Action, int Row, int Column, const QModelIndex & Parent)
    2. {
    3. if (/*some_error_condition*/)
    4. {
    5. setErrorFlag(true);
    6. return false;
    7. }
    8. setErrorFlag(false);
    9. return QAbstractItemModel::dropMimeData ( pData, Action, Row, Column, Parent );}
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void TMyTreeView::dropEvent ( QDropEvent * pEvent )
    2. {
    3. QTreeView::dropEvent(aEvent);
    4.  
    5. // this cancels removeRows
    6. TMyModel *pModel = dynamic_cast<TMyModel *>(model());
    7. if (pModel != NULL)
    8. {
    9. if (pModel->getErrorFlag())
    10. {
    11. pEvent->setDropAction(Qt::IgnoreAction);
    12. }
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    It works. But such a decision seems to be unobvious...
    Last edited by Miga; 11th July 2012 at 10:55. Reason: error in code

Similar Threads

  1. Replies: 4
    Last Post: 18th April 2012, 18:11
  2. How to remove row from Model without removing from source
    By poporacer in forum Qt Programming
    Replies: 5
    Last Post: 2nd May 2011, 16:43
  3. QFileSystemModel::remove not removing index from model
    By revorgm in forum Qt Programming
    Replies: 1
    Last Post: 27th February 2011, 09:24
  4. removing model Items
    By gyre in forum Newbie
    Replies: 2
    Last Post: 25th November 2007, 20:10
  5. Removing rows from table model
    By steg90 in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2007, 20:36

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.