Qt Drag and Drop QListView removing the item on which it is released

I working on a small QlistView which is Sortable.

    iListView = new QListView(this);
    //Creating a standard item model
    iStandardModel = new QStandardItemModel(this);

    //First item
    QStandardItem* item1 = new QStandardItem(QIcon(":/cover-story-album-art.jpg"),"First Item");
    //Second item
    QStandardItem* item2 = new QStandardItem(QIcon(":/cover-story-album-art.jpg"),"Second item");
    //third item 
    QStandardItem* item3 = new QStandardItem(QIcon(":/cover-story-album-art.jpg"),"Third![enter image description here][1] item");

    //Appending the items into model
    iStandardModel->appendRow(item1);
    iStandardModel->appendRow(item2);
    iStandardModel->appendRow(item3);

    //Setting the icon size
    iListView->setIconSize(QSize(40,30));

    //Setting the model
    iListView->setModel(iStandardModel);

    //Setting listview geometry
    iListView->setGeometry(QRect(0,0,240,320));
    iListView->setDragEnabled(true);
    iListView->setAcceptDrops(true);
    iListView->setDragDropMode(QAbstractItemView::InternalMove);

Well the Drag and Drop works but there is a issuse if i drop the item on the any other item replaced other than the end of the list.The "dragged" item replaces the "released up on" item.

Screen shot of QListView at different Scenarios

Answers


That is because by default QStandardItem has Qt::ItemIsDropEnabled flag set. Just remove it by using QStandardItem::setFlags() function. Add following lines:

item1->setFlags(item1->flags() ^ (Qt::ItemIsDropEnabled));
item2->setFlags(item2->flags() ^ (Qt::ItemIsDropEnabled));
item3->setFlags(item3->flags() ^ (Qt::ItemIsDropEnabled));

iListView->showDropIndicator(); // For convenience..

Need Your Help

Variable table name in dynamic sql with COALESCE

sql-server stored-procedures prepared-statement dynamic-sql

I want to get columnnaes of a table in a variable in this form col1,col2,col3

Check Map key/values with jsonPath

java jsonpath mockmvc

I'm testing a controller that returns a Map