【发布时间】:2016-03-28 19:54:12
【问题描述】:
我正在尝试将项目(纯/文本)从一个 QListView 拖放到另一个。拖动开始得很好(我什至可以将项目拖放到另一个接受文本拖放的应用程序),但我的第二个 QListView 出于某种原因不接受拖放。 以下是列表视图的配置方式:
ui->lessonsListView->setAcceptDrops(true);
ui->lessonsListView->setDropIndicatorShown(true);
ui->lessonsListView->setDragDropMode(QAbstractItemView::DropOnly);
ui->lessonsListView->setDragDropOverwriteMode(true);
此 listView 的代理模型实现以下方法:
Qt::ItemFlags LessonsProxyModel::flags(const QModelIndex &index) const
{
qDebug() << __FUNCTION__;
return Qt::ItemIsDropEnabled | QSortFilterProxyModel::flags(index);
}
Qt::DropActions LessonsProxyModel::supportedDropActions() const
{
qDebug() << __FUNCTION__;
return Qt::MoveAction;
}
bool LessonsProxyModel::canDropMimeData(const QMimeData *data,
Qt::DropAction action, int row, int column, const QModelIndex &parent)
{
qDebug() << __FUNCTION__;
Q_UNUSED(action);
Q_UNUSED(row);
Q_UNUSED(column);
if (!data->hasFormat("text/plain") || !parent.isValid())
return false;
return true;
}
bool LessonsProxyModel::dropMimeData(const QMimeData *data,
Qt::DropAction action, int row, int column, const QModelIndex &parent)
{
qDebug() << __FUNCTION__;
if (!canDropMimeData(data, action, row, column, parent))
return false;
emit dataDropped(data, parent);
return true;
}
从应用程序输出中,我看到只有 supportedDropActions() 和 flags() 被调用。 canDropMimeData() 和 dropMimeData() 都没有打过电话。我究竟做错了什么?
任何提示将不胜感激。
谢谢!
已编辑:
以防万一:下面是 listView 的源代码,并且从这些拖动中启动模型: 列表视图设置:
ui->abonsListView->setDragEnabled(true);
代理型号代码:
Qt::ItemFlags AbonsProxyModel::flags(const QModelIndex &index) const
{
return Qt::ItemIsDragEnabled | QSortFilterProxyModel::flags(index);
}
Qt::DropActions AbonsProxyModel::supportedDragActions() const
{
qDebug() << __FUNCTION__;
return Qt::MoveAction;
}
QStringList AbonsProxyModel::mimeTypes() const
{
qDebug() << __FUNCTION__;
QStringList types;
types << "text/plain";
return types;
}
QMimeData *AbonsProxyModel::mimeData(const QModelIndexList &indexes) const
{
qDebug() << __FUNCTION__;
QMimeData *mimeData = new QMimeData();
foreach (const QModelIndex &index, indexes)
if (index.isValid())
{
mimeData->setText(data(index, AbonsModel::Id).toString());
qDebug() << __FUNCTION__;
return mimeData;
}
return mimeData;
}
【问题讨论】:
-
我相信,您必须覆盖
QListView子类的dragEnterEvent、dragMoveEvent和dropEvent。看看documentation 和examples -
谢谢!我会尝试。但上述文档指出:
This document describes the basic drag and drop mechanism and outlines the approach used to enable it in custom controls.而我只是按照Using drag and drop with item views链接下的说明进行操作。对于另一个没有任何子类化的列表视图来说,拖动也很完美:项目视图已经支持拖放。 -
是的,拖动完美实现。然而,下降没有按预期工作。我尝试使用
QListView加上QFileSystemModel和文件浏览器(海豚)进行简单的拖放实验。从QListView到 dolphin 的拖动效果非常好,但不接受反过来。所以我相信,你必须为你要执行放置的小部件设置dragEnterEvent、dragMoveEvent和dropEvent。 -
Drop to
QListView工作正常,您不需要任何子类。显示代码,您在其中配置QListView,开始拖动。 -
我已经发布了“拖拽侧”的代码。顺便说一句,我发现 Qt 源代码,
canDropMimeData()之类的函数没有在QAbstractProxyModel中声明virtual,并且由于这个错误的函数被调用:QAbstractProxyModel::canDropMimeData()而不是我模型的canDropMimeData()。似乎这就是原因,但我不知道如何修复或解决它......似乎它只适用于QAbstractItemModel。
标签: qt drag-and-drop qlistview qsortfilterproxymodel