Results 1 to 16 of 16

Thread: QListView drag and drop

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2020
    Posts
    53
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QListView drag and drop

    I thought I had fixed this, but discovered that if I drag an item (string) from listA and drop it over an existing item in listB then the existing item is overwritten. From the docs it seems that this behaviour can be prevented by calling setDragDropOverwriteMode(false), but that doesn't change anything. What is the correct way to prevent overwriting an existing item?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,349
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: QListView drag and drop

    According to the QAbstractItemView docs:

    dragDropOverwriteMode : bool

    This property holds the view's drag and drop behavior

    If its value is true, the selected data will overwrite the existing item data when dropped, while moving the data will clear the item. If its value is false, the selected data will be inserted as a new item when the data is dropped. When the data is moved, the item is removed as well.

    The default value is false, as in the QListView and QTreeView subclasses. In the QTableView subclass, on the other hand, the property has been set to true.

    Note: This is not intended to prevent overwriting of items. The model's implementation of flags() should do that by not returning Qt::ItemIsDropEnabled.
    The last sentence (Note) is the important one. If you have already derived from QStringListModel to implement supportedDropActions(), then you will also need to implement the flags() method in your model class to mask out the ItemIsDropEnabled flag. ( return QStringListModel::flags( index ) & ~Qt::ItemIsDropEnabled; ) [Edit: added "index" argument to superclass call]

    This doesn't make a lot of sense to me. It sounds like dragDropOverwriteMode applies to the entire model when something is dropped, not individual items, but if the default is true for QTableView, who would want behavior when dropping something erased the entire table?
    Last edited by d_stranz; 20th August 2020 at 22:55.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jan 2020
    Posts
    53
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QListView drag and drop

    Quote Originally Posted by d_stranz View Post
    According to the QAbstractItemView docs:



    The last sentence (Note) is the important one. If you have already derived from QStringListModel to implement supportedDropActions(), then you will also need to implement the flags() method in your model class to mask out the ItemIsDropEnabled flag. ( return QStringListModel::flags() & ~Qt::ItemIsDropEnabled; )

    This doesn't make a lot of sense to me. It sounds like dragDropOverwriteMode applies to the entire model when something is dropped, not individual items, but if the default is true for QTableView, who would want behavior when dropping something erased the entire table?
    Do you mean?
    Qt Code:
    1. virtual Qt::ItemFlags flags(const QModelIndex &index) const override
    2. {
    3. return QStringListModel::flags() | ~Qt::ItemIsDropEnabled;
    4. }
    To copy to clipboard, switch view to plain text mode 
    Probably not, as it doesn't compile.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,349
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: QListView drag and drop

    No, you should be returning the logical AND of whatever flags there already are (QStringListModel:: flags()) with the bitwise NOT of Qt:: ItemIsDropEnabled. My original post had a logical OR, and I realized the mistake and fixed it a bit later.

    Qt Code:
    1. return QStringListModel::flags( index ) & ~Qt::ItemIsDropEnabled;
    To copy to clipboard, switch view to plain text mode 

    Your code doesn't compile because you left out the "index" argument to the superclass call. In this case, I also left out the argument, but seriously, you have to look at what the compiler is telling you and figure out what is wrong.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Jan 2020
    Posts
    53
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QListView drag and drop

    Quote Originally Posted by d_stranz View Post
    No, you should be returning the logical AND of whatever flags there already are (QStringListModel:: flags()) with the bitwise NOT of Qt:: ItemIsDropEnabled.
    Qt Code:
    1. return QStringListModel::flags( index ) & ~Qt::ItemIsDropEnabled;
    To copy to clipboard, switch view to plain text mode 
    Yes, you are right. However, this prevents all drop actions in the view. The solution to the problem, which can be found here, is to AND in Qt::ItemIsDropEnabled only if (index.isValid()). So now the problem is fixed.

Similar Threads

  1. Need help regarding drag and drop in QListView.
    By sonulohani in forum Qt Programming
    Replies: 6
    Last Post: 8th May 2016, 09:13
  2. qt 3.3 qlistview drag and drop
    By harishiva in forum Qt Programming
    Replies: 0
    Last Post: 21st September 2011, 12:46
  3. Replies: 2
    Last Post: 13th October 2010, 21:51
  4. Drag and Drop from QListView to QGraphicsView
    By NoRulez in forum Qt Programming
    Replies: 0
    Last Post: 20th August 2009, 14:26
  5. Problem with drag&drop in qlistview
    By mambo in forum Qt Programming
    Replies: 2
    Last Post: 11th September 2006, 16:14

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.