Qt拖放QListView删除发布其的项目 [英] Qt Drag and Drop QListView removing the item on which it is released

查看:329
本文介绍了Qt拖放QListView删除发布其的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个可排序的小型QlistView.

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.

不同情况下QListView的屏幕截图

推荐答案

这是因为默认情况下QStandardItem设置了Qt::ItemIsDropEnabled标志.只需使用QStandardItem::setFlags()函数将其删除.添加以下行:

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

这篇关于Qt拖放QListView删除发布其的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆