Results 1 to 5 of 5

Thread: List-centred table view: How to implement DND and moving rows.

  1. #1
    Join Date
    Jul 2008
    Posts
    16
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default List-centred table view: How to implement DND and moving rows.

    Hi,
    I want to implement some sort of playlist for a media player and ran into a few problems there. So, I’ve got several media items and number of properties like the name and length and so on for them. The media items should be structured in rows and the properties should be in the columns. Sorting by columns should be possible as well as moving the media items around by mouse dragging and dropping. I guess you know, what I’m talking about.

    So, first of all, I’m unsure, how to implement all of this. I settled with QAbstractTableModel and QTableView but since my data is not really organised in a table but in a list of rows with certain properties, I do not know, if this approach is correct or if I should have used some other classes in the first place.

    Now, concerning the dragging of items, I use the approach of having a Qt::UserRole set which will copy an internal serialisable data structure with the complete data of one row each time a drag is performed.

    The problem is, when I set the flags to
    Qt Code:
    1. Qt::ItemFlags flags(const QModelIndex &index) const {
    2. Qt::ItemFlags defaultFlags = QAbstractTableModel::flags(index);
    3. if (index.isValid())
    4. return Qt::ItemIsDragEnabled | defaultFlags;
    5. else
    6. return Qt::ItemIsDropEnabled | defaultFlags;
    To copy to clipboard, switch view to plain text mode 
    the code is copied as many times as the current column number is.
    When I include a
    Qt Code:
    1. if (index.column() != 0) return defaultFlags;
    To copy to clipboard, switch view to plain text mode 
    however, it looks as if I’m only dragging the first cell in the row.
    Also, on some occasions, the row will be lost, when I drag the mouse to the wrong corner. What can I do about that?

    I have given the rest of the code in an attachment, so if anyone could give me some advice on how to do this the right way, I’d be very glad.

    /rike
    Attached Files Attached Files

  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: List-centred table view: How to implement DND and moving rows.

    Change the "selectionBehavior" property of the view to "SelectRows". Then you will always be dragging complete rows, if you need it. But on the other hand you don't have to do that. You are the one who decides what the drag contains by reimplementing appropriate methods in the model so you can fit data from all columns into a single drag.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jul 2008
    Posts
    16
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: List-centred table view: How to implement DND and moving rows.

    Yes, this is what I have done already. But the thing is, that it will copy the row several times and not just move it. How do I turn this off, then?

  4. #4
    Join Date
    Jul 2008
    Posts
    16
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: List-centred table view: How to implement DND and moving rows.

    Hmm. Ok, I have found a way to avoid the duplication. In my model, I added:

    Qt Code:
    1. bool dropMimeData(const QMimeData *data, Qt::DropAction action,
    2. int row, int column, const QModelIndex &parent) {
    3. return QAbstractTableModel::dropMimeData(data, action,
    4. row, 0, parent);
    5. }
    To copy to clipboard, switch view to plain text mode 

    Doesn’t look to smart to me, but it keeps the rows ‘aligned’ when dragging and dropping.

  5. #5
    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: List-centred table view: How to implement DND and moving rows.

    It basically depends on the drop action you perform. If the action is DropMove, you should remove the old item after the drop.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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.