[Solved] QListView: ReOrdering list Items using Drag and Drop.

  • I am implementing the Drag and Drop functionality in a listView, I implemented the methods given in the documentation , the drag and drop works correctly , However the listitem are not ordered properly. I have also subclassed QStandardItemModel and overridden

    @Qt::ItemFlags flags(const QModelIndex &index) const;
    Qt::DropActions supportedDropActions() const;@

    I searched for the same, but couldnt get any specific detail regarding the implementation.

    Any help will be highly appreciated.


  • Hi,

    if you want to use it for reordering, I think you have to implement the move for drag and drop.

  • Do I need to implement this for my listView or for the model ? I have a custom ListView and a custom model that inherits QStandardItemModel.

  • I am sorry Andre, I dont want to create a new thread, I just got a bit confused . Should I use a single topic for an entire requirement or should I seperate each topic as I did here.

    I dont have any problem if all the topics are merged at a single place. I cant delete all the other threads that I have created, If it is against the rules of the forum I humbly request you (moderator) to take necessary actions/delete the other topics as well.

  • You keep on making topics for aspects of your problem, without ever giving the big picture. That is very annoying. It seems that you want to achieve some bigger goal, but you have no clue how to do it. Or rather, you are just trying different approaches and ask about your issues with those, instead of asking about your real problem. Rather like "this":http://mywiki.wooledge.org/XyProblem fenomenon. This is also an "item":http://www.catb.org/esr/faqs/smart-questions.html#goal in the FAQ on how to ask smart questions.

    So: first be clear about what you are trying to achieve, then discribe how you tried to achieve that, and what your problems were with that approach (or those approaches). Then ask for suggestions on how to solve your actual issue.

  • Thanks for the explanations, I understand that I created the similar XY problem specified in the link. I'll try to ask questions that are well prepared, easily understood and less annoying.

    For the actual question i.e reordering the listview items using drag and drop, I have implemented the code that runs as expected. I'll update the code after a while.


  • So far the drag and drop functionality works as expected and the item are arranged properly in the listView. I have also tried to implement a scrolling functionality that scrolls the listView up/down based on the visible row(top row n bottom row) in the viewport->rect() while dragging an item.

    I am changing the verticalScrollBar value inside the dragMoveEnter() event

    @QScrollBar *scroll = verticalScrollBar();
    if (currentRow == topRow)
    scroll->setValue(scroll->value() -1);

    } else if (currentRow == bottomRow)
    scroll->setValue(scroll->value() +1);

    where currentRow is the row where the mouse position is while dragging the item.

    However the scroll bar value changes only on mouseMove / dragMove , means if i have 1000 items in the list then at the last row in the visible viewport() i have to keep on moving the mouse to scroll down, But I want to create a functionality like if the item is dragged and is at the top/bottom row in the visible region then the scroll bar should move up/down automatically.

    Is it possible to get continuous events or a sensitve area that fires the signal continuously if mouseOver?

  • Ok one more question,

    My ListView contains a customWidget which is added using setIndexWidget(),where customWidget has few components like a combobox + lineEdits + labels , So far I have installed eventFilter for my customWidget that works fine, How can i set eventFilters for my listView so that If any widget on the customWidget is selected then the currentIndex/currentItem in the listView gets highlighted/selected.


    Edit :- Implemented using signal, slots and eventFilters().

  • Mr Andre,

    I am unable to drag and drop with link action. If understand well, with link action, we can drag the references of the items so that any changes made in the source items will reflect the destination items(which is my requirement). Unfortunately, I couldn't succeed. If this is the big topic then i would open a new thread.


Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.