Results 1 to 2 of 2

Thread: Drag and drop items in view to sort order

  1. #1
    Join Date
    May 2006
    Posts
    28
    Thanks
    8
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Drag and drop items in view to sort order

    Hi,

    I'd like to sort items in a tree view by dragging and dropping.
    My data is a flat list of rows and columns with no heirarchy. I chose not to use TableView as the row headers cant be hidden, is this right ?

    I've got as far as subclassing standardItemView and re implementing :
    supportedDropActions() const
    removeRows(int row, int count, const QModelIndex &parent)

    My trouble is what to put in removeRows so that a MoveAction is done on the model. At the moment I get copying of data in the view.

    Another problem is that when dragging the treeView item, I get a row with a + as its a treeView Im using, any ideas where I'm going wrong.

    Below is what I've got so far


    Qt Code:
    1. MyModel::MyModel(QObject *parent)
    2. {
    3. }
    4.  
    5. Qt::DropActions MyModel::supportedDropActions() const
    6. {
    7. return Qt::MoveAction;
    8. }
    9.  
    10. bool MyModel::removeRows(int row, int count, const QModelIndex &parent)
    11. {
    12. if (parent.isValid())
    13. return false;
    14. beginRemoveRows(parent, 0, 0);
    15. // What am I doing here?
    16. endRemoveRows();
    17. return true;
    18. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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 items in view to sort order

    Quote Originally Posted by Big Duck
    Hi,

    I'd like to sort items in a tree view by dragging and dropping.
    My data is a flat list of rows and columns with no heirarchy. I chose not to use TableView as the row headers cant be hidden, is this right ?
    Qt Code:
    1. tableView->horizontalHeader()->hide();
    To copy to clipboard, switch view to plain text mode 

    I've got as far as subclassing standardItemView and re implementing :
    supportedDropActions() const
    removeRows(int row, int count, const QModelIndex &parent)
    You don't have to reimplement removeRows(). You have to reimplement removeRow(). the former will call removeRow() for each row which is to be removed, so you don't have to subclass it. removeRow() should... remove a row pointed by the index from the model (meaning its internal representation). For example, if you hold your items in a QList, you should remove an item associated with the index from that list.

    My trouble is what to put in removeRows so that a MoveAction is done on the model. At the moment I get copying of data in the view.
    Look above.

    Another problem is that when dragging the treeView item, I get a row with a + as its a treeView Im using, any ideas where I'm going wrong.
    That's because the item is copied as a child of the item you drop it on. You should reimplement dropMimeData() to correct that.

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

    Big Duck (2nd June 2006)

Similar Threads

  1. Drag & drop with model/view (Qt4)
    By yogeshm02 in forum Qt Programming
    Replies: 16
    Last Post: 19th September 2011, 21:36
  2. Drag and drop outside the application
    By jpn in forum Newbie
    Replies: 7
    Last Post: 27th August 2006, 16:37
  3. drag and drop with item views
    By castorvert in forum Qt Programming
    Replies: 14
    Last Post: 27th March 2006, 11:12
  4. Drag and Drop (drop example)
    By din9 in forum Qt Programming
    Replies: 1
    Last Post: 23rd January 2006, 19:03
  5. Drag 'n Drop problem
    By kiker99 in forum Qt Programming
    Replies: 4
    Last Post: 16th January 2006, 17:35

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.