Results 1 to 3 of 3

Thread: How to reorder items in QListView?

  1. #1
    Join Date
    Jan 2009
    Posts
    20
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X

    Default How to reorder items in QListView?

    I want to rearrange/reorder items by dragging and dropping - which I can do, but with some unwanted side-effects. I've tried adjusting numerous options in order to fix the problem, but to no avail. This is now driving me crazy. Any help would be greatly appreciated.

    I'm using model/view classes (QStringListModel and QListView) and can rearrange the items, e.g. drag "ID" to the top of the list. I can do that, but only if I'm careful. The unwanted side-effect is that I can easily overwrite another item if my mouse is not placed exactly between the items. For example, if I drag "ID" and drop it over "Name", the list will shrink, with "Name" nowhere to be seen again.

    I've also tried subclassing QStringListModel and changing flags, but still no luck.

    How can I eliminate this overwriting effect? I just want pure and simple reordering without losing any items.

    A sample code is below. Thanks.

    Qt Code:
    1. #include <QtGui>
    2. #include <QApplication>
    3.  
    4. class MyListModel : public QStringListModel
    5. {
    6. public:
    7. MyListModel(const QStringList &strings, QObject *parent = 0 );
    8. Qt::ItemFlags flags(const QModelIndex&index) const;
    9. };
    10.  
    11. MyListModel::MyListModel(const QStringList &strings, QObject *parent) : QStringListModel(strings, parent)
    12. {
    13. }
    14.  
    15. Qt::ItemFlags MyListModel::flags(const QModelIndex&index) const
    16. {
    17. Qt::ItemFlags flags = QStringListModel::flags(index);
    18. // flags = Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled;
    19. flags = Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEnabled;
    20. return flags;
    21. }
    22.  
    23.  
    24.  
    25. int main(int argc, char *argv[])
    26. {
    27. QApplication a(argc, argv);
    28.  
    29. QListView list;
    30. list.setViewMode(QListView::ListMode);
    31. list.setMovement(QListView::Snap);
    32. list.setDragDropMode(QAbstractItemView::InternalMove);
    33. list.setDragDropOverwriteMode(false);
    34.  
    35. QStringListModel model(QStringList() << "Name" << "Surname" << "ID");
    36. // MyListModel model(QStringList() << "Name" << "Surname" << "ID");
    37. list.setModel(&model);
    38. list.show();
    39.  
    40. return a.exec();
    41. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to reorder items in QListView?

    You still want the root index to accepts drops, just not the items so try:
    Qt Code:
    1. Qt::ItemFlags MyListModel::flags(const QModelIndex&index) const
    2. {
    3. Qt::ItemFlags flags; //= QStringListModel::flags(index);
    4.  
    5. if (index.isValid())
    6. flags = Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled;
    7. else
    8. flags = Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEnabled;
    9.  
    10. return flags;
    11. }
    To copy to clipboard, switch view to plain text mode 
    This will work if the root index is invalid (as it usually is). However, if you use setRootIndex you may have to compare against that index instead.

  3. The following user says thank you to numbat for this useful post:

    xfurrier (12th August 2009)

  4. #3
    Join Date
    Jan 2009
    Posts
    20
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: How to reorder items in QListView?

    exactly what I wanted. thanx.

Similar Threads

  1. Removing all items QListView
    By vcp in forum Qt Programming
    Replies: 2
    Last Post: 6th August 2009, 18:55
  2. how to get item's text or path from QListVIew
    By Mystical Groovy in forum Qt Programming
    Replies: 4
    Last Post: 5th September 2008, 01:25
  3. reorder items in QListView/QTreeView
    By sreedhar in forum Qt Programming
    Replies: 3
    Last Post: 27th July 2008, 12:50
  4. Items in QListView should sort on Header Click
    By vinnu in forum Qt Programming
    Replies: 14
    Last Post: 10th November 2006, 12:49
  5. moving Qlistview items
    By :db:sStrong in forum Qt Programming
    Replies: 0
    Last Post: 21st February 2006, 12:25

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.