Results 1 to 15 of 15

Thread: drag and drop with item views

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 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...

  2. #2
    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.

  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

    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 ...

  4. #4
    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 

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 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.

  6. #6
    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 ?

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 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().

  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

    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

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

    tpf80 (21st December 2008)

  10. #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

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

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 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, 16:56
  2. Replies: 1
    Last Post: 18th November 2008, 06:57
  3. Drag and Drop item inside QTreeWidget
    By nina1983 in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2008, 11:43
  4. Drag and drop between model views
    By larry104 in forum Qt Programming
    Replies: 20
    Last Post: 19th January 2008, 16:09
  5. Drag an item from QTreeWidget and drop in TableView
    By steg90 in forum Qt Programming
    Replies: 8
    Last Post: 18th May 2007, 11: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
  •  
Qt is a trademark of The Qt Company.