Results 1 to 17 of 17

Thread: Drag & drop with model/view (Qt4)

  1. #1
    Join Date
    Jan 2006
    Location
    India
    Posts
    115
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Arrow Drag & drop with model/view (Qt4)

    Hi

    I have two suggestions/ideas/requests (you choose the correct one!) regarding drag & drop with model/view classes, for the Qt developers: -
    1. While moving an item in a view using drag & drop, insertRows() and removeRows() functions are used. Can't models have single function (may be moveRows()) to achieve this?
    2. Can drag&drop action from one view to another, both having same models, be made to work like a move operation?


    What do you think?
    Do Qt developers have an eye on this forum or I'll have to submit a wish (Can I?)?

    I am using model/view classes extensively in my application. Models in my app are not only used to store data but are also used for anything which needs to be done with it and at times two views display data from single model.

    Also, those who are working with custom models may have noticed very buggy behaviour of drag & drop operations. Right? Or there is there something seriously wrong with my implementation? I tried hard but failed to generate a smaller variation of my model to show you code.

    With regards

  2. #2
    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: Drag & drop with model/view (Qt4)

    Quote Originally Posted by yogeshm02
    I have two suggestions/ideas/requests (you choose the correct one!) regarding drag & drop with model/view classes, for the Qt developers: -

    - While moving an item in a view using drag & drop, insertRows() and removeRows() functions are used. Can't models have single function (may be moveRows()) to achieve this?
    They probably could, but would it differ from using removeRows() and insertRows() in sequence? Now, because there are more than one drag operations (move, copy and link), it would make sense for all of them to have an own method -- moveRows(), copyRows(), linkRows() which would make the model framework more complicated.

    - Can drag&drop action from one view to another, both having same models, be made to work like a move operation?
    They could. But what about operations, which do not come from a view? It could make those operations more complex (one would have to implement many situations -- view A -- view A, view A - view B, non-view - view, view - non-view, non-view - non-view -- maybe even more).

    Do Qt developers have an eye on this forum or I'll have to submit a wish (Can I?)?
    They sometimes do, but you should submit a report anyway.

    I am using model/view classes extensively in my application. Models in my app are not only used to store data but are also used for anything which needs to be done with it and at times two views display data from single model.
    That's how they should be used, right?

    Also, those who are working with custom models may have noticed very buggy behaviour of drag & drop operations. Right? Or there is there something seriously wrong with my implementation? I tried hard but failed to generate a smaller variation of my model to show you code.
    Drag and drop is really associated with views, not models. This could be an argument against implementing move/copy/linkRows() into the model. It should be as simple as possible. The Interview framework is not perfect. If you try to explain us your problems, maybe we'll be able to help.

  3. #3
    Join Date
    Jan 2006
    Location
    India
    Posts
    115
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drag & drop with model/view (Qt4)

    Quote Originally Posted by wysota
    They probably could, but would it differ from using removeRows() and insertRows() in sequence? Now, because there are more than one drag operations (move, copy and link), it would make sense for all of them to have an own method -- moveRows(), copyRows(), linkRows() which would make the model framework more complicated.
    But that would give additional power to models, e.g. my implemented model uses custom function for inserting data and inserting data in model take some time. So, either I will do re-validation of data in each insertRow() or assume that everything is always correct. If my suggestion is implemented, model can decide whether rows can be inserted/moved/linked.

    Quote Originally Posted by wysota
    They could. But what about operations, which do not come from a view? It could make those operations more complex (one would have to implement many situations -- view A -- view A, view A - view B, non-view - view, view - non-view, non-view - non-view -- maybe even more).
    Now it looks very hard! Though I can tweak my model to behave like this because items in my model are unique, so it will be easy for me to implement (with some assumptions).

    Quote Originally Posted by wysota
    Drag and drop is really associated with views, not models. This could be an argument against implementing move/copy/linkRows() into the model. It should be as simple as possible. The Interview framework is not perfect. If you try to explain us your problems, maybe we'll be able to help.
    I was unable to downgrade my implementation so I started from scratch hoping standard classes will prove buggy, and I tasted success. Example is attached (20 lines only).

    Just try to move any item to the point where all items end and see the results like in the screenshots.
    This sometimes also happens when items are dropped between two items (occurs very rarely) and also sometimes two rows are inserted in that place. I am unable to get that done ATM.
    Attached Images Attached Images
    Attached Files Attached Files

  4. #4
    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: Drag & drop with model/view (Qt4)

    Quote Originally Posted by yogeshm02
    But that would give additional power to models, e.g. my implemented model uses custom function for inserting data and inserting data in model take some time. So, either I will do re-validation of data in each insertRow() or assume that everything is always correct. If my suggestion is implemented, model can decide whether rows can be inserted/moved/linked.
    This can be implemented on the view level. Check if target and source are the same and handle the input accordingly.

  5. #5
    Join Date
    Jan 2006
    Location
    India
    Posts
    115
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drag & drop with model/view (Qt4)

    Quote Originally Posted by yogeshm02
    I was unable to downgrade my implementation so I started from scratch hoping standard classes will prove buggy, and I tasted success. Example is attached (20 lines only).

    Just try to move any item to the point where all items end and see the results like in the screenshots.
    This sometimes also happens when items are dropped between two items (occurs very rarely) and also sometimes two rows are inserted in that place. I am unable to get that done ATM.
    Is it really a bug? Please reply so that I it could be submitted to qt folks.

  6. #6
    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: Drag & drop with model/view (Qt4)

    I don't know. You can report it to the Trolls and they will confirm or deny that.

  7. #7
    Join Date
    Jun 2010
    Posts
    15
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag & drop with model/view (Qt4)

    I have another question about drag&drop inside MVC framework.
    I need simply move rows inside QtableView,
    I go along proposed in docs way - reimplement: mimeType, mimeData, dropMimeData.

    But i need such behaviour: when I start dragging row, I need remove this row from table,
    then after droping put them in new place, or return to original position if droping was out of widget bounds.
    But in what function I can call models func - removeRows? I cann't do this in mimeData because it const.
    Where correct place to initialy(on dragging start) remove row?

    Thank you!

  8. #8
    Join Date
    Jun 2010
    Posts
    15
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag & drop with model/view (Qt4)

    Is there no one has done such before?

  9. #9
    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: Drag & drop with model/view (Qt4)

    What you require is somewhat against traditional approach to drag and drop. What happens if you start a drag (hence remove the item) and then cancel the drag without dropping the item on the widget or if you drop the item on some external application? The drop is traditionally handled by the target and not the source. Of course you can have the behaviour you want by reimplementing mouse events and starting the drag manually (and prior to doing that you need to remove the item from the model) but then you have to handle all possible drop situations manually (and you'll have no information about WHO accepted the drop).
    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.


  10. #10
    Join Date
    Jun 2010
    Posts
    15
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag & drop with model/view (Qt4)

    Why not traditional? I use chrome browser, when I start drag tab in horizontal direction, it removes from its initial position and sticks to cursor, then I can move it where I want, of course inside tabbar, seems my approach the same.

    Athrough if I move tab outside tab bar, somewhere inside browser window, its like you say, like tab losses it parent. And then if I simply drop tab anywhere, new window will creates for this tab.

    So what I required, if I want during move row, detach it from its initial position?
    Last edited by Nickolay; 19th September 2011 at 20:04.

  11. #11
    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: Drag & drop with model/view (Qt4)

    Quote Originally Posted by Nickolay View Post
    Why not traditional?
    Because typically the operation is performed during the drop and not during the drag.

    I use chrome browser, when I start drag tab in horizontal direction, it removes from its initial position and sticks to cursor, then I can move it where I want, of course inside tabbar, seems my approach the same.
    Your approach is different. For instance you may have multiple views on the same model, removing the item from all the views handling that model is counter-intuitive.

    Athrough if I move tab outside tab bar, somewhere inside browser window, its like you say, like tab losses it parent. And then if I simply drop tab anywhere, new window will creates for this tab.
    That's not the case with your situation, is it? Furthermore look that in Chrome the drop is actually performed during the drag, the tab is not removed when you start dragging it but only when you move it past the border of the next tab (or you drag it outside the tab bar).

    As I said, you can do what you want, but since this is not a typical approach to drag and drop, there is no built-in support for it. You may implement your own view and handle it the way you want.
    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.


  12. #12
    Join Date
    Jun 2010
    Posts
    15
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag & drop with model/view (Qt4)

    Quote Originally Posted by wysota View Post
    That's not the case with your situation, is it? Furthermore look that in Chrome the drop is actually performed during the drag, the tab is not removed when you start dragging it but only when you move it past the border of the next tab (or you drag it outside the tab bar).
    What I need to reimplement in therms of MVC? I need subclass QTableView and override mouse handlers?

  13. #13
    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: Drag & drop with model/view (Qt4)

    You need to remplement mouse events and rendering-related methods (which in practice makes up most if not all of the view code).
    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.


  14. #14
    Join Date
    Jun 2010
    Posts
    15
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag & drop with model/view (Qt4)

    In very simple case, in current default implementation, to implement row moving inside one table,
    what is the best approach with functions removeRows and insertRows?
    Could I use them directly one after another inside dropMimeData?
    So I can pack in MIME number of line of dragged item and inside dropMimeData I retrieve number of drop line.

  15. #15
    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: Drag & drop with model/view (Qt4)

    Quote Originally Posted by Nickolay View Post
    what is the best approach with functions removeRows and insertRows?
    Could I use them directly one after another inside dropMimeData?
    I would say it is better to use QAbstractItemModel::beginMoveRows() and QAbstractItemModel::endMoveRows() in this case. In a general case dropMimeData() would only insert rows and rows removal would be done in the code when the drag is started when a "move" operation is detected.
    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.


  16. #16
    Join Date
    Jun 2010
    Posts
    15
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag & drop with model/view (Qt4)

    I'm not clearly understand what do you mean.
    generally I didn't get what sense of QAbstractItemModel::beginMoveRows() and QAbstractItemModel::endMoveRows()

    By docs I should use them from removeRows, like

    bool TableModel::removeRows(int position, int rows, const QModelIndex& index)
    {
    Q_UNUSED(index);
    beginRemoveRows(QModelIndex(), position, position + rows - 1);
    endRemoveRows();
    return true;
    }

    But dragging begins from const func mimeData, where I can't call non const method.

    and rows removal would be done in the code when the drag is started when a "move" operation is detected.
    Where that place you have mentioned?
    Last edited by Nickolay; 19th September 2011 at 21:37.

  17. #17
    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: Drag & drop with model/view (Qt4)

    I suggest you read a little about model-view framework in Qt. Everything should become clearer then. One hint -- drag and drop in Qt is handled in the model and not in the view.
    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.


Similar Threads

  1. Replies: 1
    Last Post: 8th January 2009, 18:40
  2. drag and drop QToolButton in QToolBar
    By NBilal in forum Qt Programming
    Replies: 1
    Last Post: 28th December 2008, 21:11
  3. Change cursor & status during Drag & Drop
    By ronlongo in forum Qt Programming
    Replies: 0
    Last Post: 1st December 2008, 17:56
  4. Drag & Drop when parent and childs accept drops?
    By gri in forum Qt Programming
    Replies: 0
    Last Post: 17th October 2008, 10:00
  5. Drag and drop revisited
    By Big Duck in forum Newbie
    Replies: 2
    Last Post: 30th June 2006, 17:41

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.