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

爷,独闯天下 提交于 2019-12-05 00:41:53

问题


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


回答1:


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..


来源:https://stackoverflow.com/questions/10990571/qt-drag-and-drop-qlistview-removing-the-item-on-which-it-is-released

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!