Results 1 to 2 of 2

Thread: QListView/QAbstractItemView and list internal drag & drop

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Join Date
    Oct 2012
    Posts
    132
    Thanks
    10
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Lightbulb Re: QListView/QAbstractItemView and list internal drag & drop

    I solved the second problem. To allow drops only outside the items and not over the items I use the following flags reimplementation in my model:
    Qt Code:
    1. Qt::ItemFlags FieldModel::flags(const QModelIndex &index) const
    2. {
    3. if (!index.isValid() || index.row() >= m_fields.count() || index.model() != this)
    4. return Qt::ItemIsDropEnabled; // we allow drops outside the items
    5. return QAbstractListModel::flags(index) | Qt::ItemIsUserCheckable | Qt::ItemIsDragEnabled;
    6. }
    To copy to clipboard, switch view to plain text mode 
    This solution is taken from qtbase/src/widgets/qlistwidget.cpp.

    Edit: I also solved the first problem. These are the correct reimplementations of insertRows and removeRows in my case:
    Qt Code:
    1. bool FieldModel::insertRows(int row, int count, const QModelIndex &parent)
    2. {
    3. if (count < 1 || row < 0 || row > rowCount() || parent.isValid())
    4. return false;
    5. beginInsertRows(QModelIndex(), row, row + count - 1);
    6. for(int index = row, end = row + count; index < end; ++index)
    7. m_fields.insert(index, p(KnownField::Invalid, false));
    8. endInsertRows();
    9. return true;
    10. }
    11.  
    12. bool FieldModel::removeRows(int row, int count, const QModelIndex &parent)
    13. {
    14. if (count < 1 || row < 0 || (row + count) > rowCount() || parent.isValid())
    15. return false;
    16. beginRemoveRows(QModelIndex(), row, row + count - 1);
    17. for(int index = row, end = row + count; index < end; ++index)
    18. m_fields.removeAt(index);
    19. endRemoveRows();
    20. return true;
    21. }
    To copy to clipboard, switch view to plain text mode 
    The problem was caused here: beginInsertRows(parent, row, 0);
    The third argument has to be row + count - 1.
    Last edited by Infinity; 15th June 2014 at 20:37.

Similar Threads

  1. QTableWidget Internal Drag/ Drop Entire Row
    By davethomaspilot in forum Newbie
    Replies: 0
    Last Post: 16th December 2012, 00:31
  2. Replies: 1
    Last Post: 6th April 2011, 07:24
  3. Replies: 2
    Last Post: 13th October 2010, 21:51
  4. Drag drop internal
    By valgaba in forum Qt Programming
    Replies: 0
    Last Post: 30th September 2010, 14:49
  5. QAbstractItemView drag & drop bug or feature?
    By wysota in forum Qt Programming
    Replies: 0
    Last Post: 13th September 2010, 22:42

Tags for this Thread

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.