Results 1 to 15 of 15

Thread: drag and drop with item views

  1. #1
    Join Date
    Mar 2006
    Posts
    11
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default drag and drop with item views

    Hello,

    Do you know when an item view uses the mimeTypes() , mimeData and dropMimeData functions ?

    I've done sth really strange :
    in my item view, in the function "mouseMoveEvent",
    I create an new QDrag and the mimeData is the following :
    code:

    Qt Code:
    1. QMimeData* m=model()->mimeData(selectedIndexes())
    To copy to clipboard, switch view to plain text mode 

    Don't you think it's done automatically ?

    Moreover, in the example "puzzle with item views", the function "dropMimeData" is defined in the model but it doesn't seem to be used. The same for mimeTypes().
    I thought it was called by the view.

    And last question : assume that I select several items, which are not following each others, can I use the function insertRows ? I can't use "insertRow" because it isn't virtual and I subclassed the model.

    Thanks,

  2. #2
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: drag and drop with item views

    Quote Originally Posted by castorvert
    Don't you think it's done automatically ?
    Yes you dont must do it manually

    Moreover, in the example "puzzle with item views", the function "dropMimeData" is defined in the model but it doesn't seem to be used. The same for mimeTypes().
    I thought it was called by the view.
    Yes it called by view

    p.s.
    last questions i'm not understand
    a life without programming is like an empty bottle

  3. #3
    Join Date
    Mar 2006
    Posts
    11
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: drag and drop with item views

    So, is it necessary to create a QDrag object in the function "mouseMoveEvent" of the view ?

    My last question was that, in my opinion, removeRows only delete rows who are following each others, and it might be not possible to use it when they are not following each others.

  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 and drop with item views

    Quote Originally Posted by castorvert
    Do you know when an item view uses the mimeTypes() , mimeData and dropMimeData functions ?
    Sure. During drag & drop mimeTypes() returns a list of supported MIME types for the model (I assume they are the ones which may be dropped onto a view supporting this model), mimeData() returns a QMimeData pointer to an object containing item representation in different MIME formats and dropMimeData() handles a drop on the model level. You should reimplement mimeData() if you want to enable dragging from the model and the other two to enable dropping on the model.

    I've done sth really strange :
    in my item view, in the function "mouseMoveEvent",
    I create an new QDrag and the mimeData is the following :
    code:

    Qt Code:
    1. QMimeData* m=model()->mimeData(selectedIndexes())
    To copy to clipboard, switch view to plain text mode 

    Don't you think it's done automatically ?
    Do you use one of the default views? If so, you don't have to do anything with above methods to make D&D work. It should be enough to enable dragging and dropping in respective widgets and make sure the model returns correct data for those three methods you mentioned in the beginning.

    Moreover, in the example "puzzle with item views", the function "dropMimeData" is defined in the model but it doesn't seem to be used. The same for mimeTypes().
    I thought it was called by the view.
    mimeTypes() should be called when you drag an item and hold it over a view. The view then asks the model if it should accept the drag or not (if it should show the "stop" sign or not). dropMimeData() should be called when an actual drop is made. As far as I understand it should perform operations on the model to set the desired data from the drag.

    And last question : assume that I select several items, which are not following each others, can I use the function insertRows ? I can't use "insertRow" because it isn't virtual and I subclassed the model.
    insertRow() calls insertRows() internally. You should reimplement insertRows() according to its parameters and let the model and view abstractions handle the rest. They will call your methods properly.

  5. #5
    Join Date
    Mar 2006
    Posts
    11
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: drag and drop with item views

    Do you use one of the default views?
    In fact, I subclassed QListView and also QTreeView (so I have 2 models).

  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 and drop with item views

    Models? Or views? QTreeView and QListView are views not models... And are you sure you need to subclass them? It is rarely needed...

  7. #7
    Join Date
    Mar 2006
    Posts
    11
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: drag and drop with item views

    I meant I had 2 models associated with my 2 views. I'll try not to subclass my views but to use the original ones. In fact, I thought I had to define custom functions for drag and drop in my views, that's why I subclassed the Qt views.

  8. #8
    Join Date
    Mar 2006
    Posts
    11
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: drag and drop with item views

    Well, I thought about it, and in my opinion, even if I don't define custom functions, it's better to subclass the view because when I create my object, I can define the delegate, the colour, the setDropEnabled ...

  9. #9
    Join Date
    Mar 2006
    Posts
    11
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: drag and drop with item views

    I found sth strange : when I subclass QAbstractListModel, I have to subclass flags() to have Drag and drop enabled. In QAbstractItemModel, there is only :

    Qt Code:
    1. Qt::ItemFlags QAbstractItemModel::flags(const QModelIndex &index) const
    2. {
    3. if (!index.isValid())
    4. return 0;
    5.  
    6. return Qt::ItemIsSelectable|Qt::ItemIsEnabled;
    7. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    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 and drop with item views

    Quote Originally Posted by castorvert
    Well, I thought about it, and in my opinion, even if I don't define custom functions, it's better to subclass the view because when I create my object, I can define the delegate, the colour, the setDropEnabled ...
    You don't have to subclass to do that. You use that view only once, so there is no point is subclassing.

    I found sth strange : when I subclass QAbstractListModel, I have to subclass flags() to have Drag and drop enabled.
    There is nothing strange in it. flags() from abstract item model just implements the most common behaviour. If you want to have a more complicated model, you have to reimplement flags() along with other modelling methods.

  11. #11
    Join Date
    Mar 2006
    Posts
    11
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: drag and drop with item views

    Thank you, i've finally understood what mimeTypes() is.
    I have another question, if you please.

    Let's take an example :

    -I defined supportedDropActions() in a model as following :
    Qt Code:
    1. Qt::DropActions ModeleDispos::supportedDropActions() const{
    2. return Qt::MoveAction;
    3. }
    To copy to clipboard, switch view to plain text mode 
    -I defined removeRows too.
    -In "QAbstractItemView", it seems that "startDrag" and "dropEvent" use "supportedDropActions" so I think it's possible to move an item into the same view.
    -the mimeTypes and mimeData are of the same Type.
    but I couldn't move an item into the model.

    It worked when I did :
    Qt Code:
    1. Qt::DropActions ModeleDispos::supportedDropActions() const{
    2. return Qt::MoveAction|Qt::CopyAction;
    3. }
    To copy to clipboard, switch view to plain text mode 

    Do you know what could explain that ?

  12. #12
    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 and drop with item views

    If you just drag an item, it is considered a copy action. To make a move action one has to hold some meta key -- control, alt or shift (I don't remember which) while performing the operation. I think it's possible to force some action but I don't know if it can be done without subclassing the view. Probably it would be easier to emulate a move within one view as a copy and handle it in dropMimeData().

  13. #13
    Join Date
    Mar 2006
    Posts
    11
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: drag and drop with item views

    My solution was the following (I subclassed views) :
    Qt Code:
    1. void viewSource::mouseMoveEvent(QMouseEvent *event)
    2. {
    3. QModelIndexList liste=selectedIndexes();
    4. QMimeData *mimeData = model()->mimeData(liste);
    5. QDrag *drag = new QDrag(this);
    6. drag->setMimeData(mimeData);
    7. drag->setHotSpot(event->pos() - rect().topLeft());
    8. Qt::DropAction dropAction = drag->start( Qt::MoveAction);
    9. if (dropAction == Qt::MoveAction) {
    10. model()->removeRows(liste[0].row(),liste.size(),liste[0].parent());
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 
    and :
    Qt Code:
    1. void viewTarget::dropEvent(QDropEvent *e) //viewTarget subclassed QListView
    2. {
    3. QListView::dropEvent(e);
    4. e->setDropAction(Qt::MoveAction);
    5. e->accept();
    6. }
    To copy to clipboard, switch view to plain text mode 

    I think it will remind you sth wysota

    Thanks for all

  14. The following user says thank you to castorvert for this useful post:

    tpf80 (21st December 2008)

  15. #14
    Join Date
    Mar 2006
    Posts
    11
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: drag and drop with item views

    Well, in fact, my "removeRows" doesn't work because the selected items aren't obligatory following each others.

  16. #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 and drop with item views

    Quote Originally Posted by castorvert
    I think it will remind you sth wysota
    Hmm... remind me what?

Similar Threads

  1. Change cursor & status during Drag & Drop
    By ronlongo in forum Qt Programming
    Replies: 0
    Last Post: 1st December 2008, 17:56
  2. Replies: 1
    Last Post: 18th November 2008, 07:57
  3. Drag and Drop item inside QTreeWidget
    By nina1983 in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2008, 12:43
  4. Drag and drop between model views
    By larry104 in forum Qt Programming
    Replies: 20
    Last Post: 19th January 2008, 17:09
  5. Drag an item from QTreeWidget and drop in TableView
    By steg90 in forum Qt Programming
    Replies: 8
    Last Post: 18th May 2007, 12:42

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.