我尝试将多个项目从窗口小部件中的视图拖放到另一个窗口小部件中的另一个视图。但是我不想丢弃已经存在的项目:例如,如果我在drop区域中有itemA和itemB,并且我拖动itemA和itemC,我想要删除itemC而不是itemA。我怎么能这样做?
我尝试使用selectionModel(),但它似乎没有用(或者我没有正确使用它):
void myWidget::dropEvent(QDropEvent * event)
{
bool dropOK;
// retrieve the source model
QObject* source = event->source();
QTreeView* viewSource = dynamic_cast<QTreeView*>(source);
QStandardItemModel* modelSource = (QStandardItemModel*)viewSource->model();
// retrieve the item dragged
QModelIndex sourceIndex = viewSource->selectionModel()->currentIndex();
QModelIndex dropIndex = indexAt(event->pos());
DropIndicatorPosition dropIndicator = dropIndicatorPosition();
/* loop for each item selected (itemA and itemC in my example) */
for (int i = 0; i < viewSource->selectionModel()->selectedIndexes().count() ; i++)
{
dropOK = true;
QStandardItem* itemSource = modelSource->itemFromIndex(viewSource->selectionModel()->selectedIndexes().at(i));
QString strUIDSource = itemSource->data(Qt::UserRole).toString();
if (dropIndex.parent().isValid() && dropIndex.row() != -1)
{
switch (dropIndicator)
{
case QAbstractItemView::AboveItem:
dropOK = true;
break;
// and so on...
}
if (dropOK == true)
{
int siblingCount = dropIndex.model()->rowCount(dropIndex.parent());
for (int iSibling = 0; iSibling < siblingCount; iSibling++)
{
QModelIndex siblingIndex = dropIndex.sibling(iSibling, 0);
QStandardItem* currentItem = m_pModel->itemFromIndex(siblingIndex);
// we compare items from source with items in the drop area according to their data
QString strUIDCurrent = currentItem->data(Qt::UserRole).toString();
if (dropIndex.parent() == viewSource->selectionModel()->selectedIndexes().at(i).parent())
dropOK = true;
else
{
//if data source == data current, we don't drop
if (strUIDSource == strUIDCurrent)
{
dropOK = false;
break;
}
else if (strUIDSource != strUIDCurrent)
dropOK = true;
}
}
}
}
else
dropOK = false;
}
if (dropOK)
QTreeView::dropEvent(event);
}
当dropOK为true时,即使我输入了&#34; if(!dropOK)&#34;还是删除了itemA和itemC。前...
提前感谢您的帮助!