I'm having problems getting my drag and drop working for a QTreeView. Everything was working fine a couple of months ago but somewhere along the line it stopped working. I've managed to get the drag and drop itself working again, but I do not get a drop indicator, and the tree doesn't expand regardless of how long I hover the drop over it.
My tree view code looks like this:
MyTreeView
::MyTreeView(QWidget *parent
) :{
viewport()->setAcceptDrops(true);
setDragEnabled(true);
setDropIndicatorShown(true);
setAutoExpandDelay(0);
setIndentation(16);
}
{
event->acceptProposedAction();
}
{
event->acceptProposedAction();
}
MyTreeView::MyTreeView(QWidget *parent) :
QTreeView(parent)
{
setSelectionMode(QAbstractItemView::SingleSelection);
viewport()->setAcceptDrops(true);
setDragEnabled(true);
setDropIndicatorShown(true);
setAutoExpandDelay(0);
setIndentation(16);
}
void MyTreeView::dragEnterEvent(QDragEnterEvent *event)
{
event->acceptProposedAction();
}
void MyTreeView::dragMoveEvent(QDragMoveEvent *event)
{
event->acceptProposedAction();
}
To copy to clipboard, switch view to plain text mode
I don't know if this code matters in this case, but this is my subclassed standard item model:
{
w = topWidget;
}
Qt::DropActions MyItemModel::supportedDropActions() const
{
return Qt::CopyAction | Qt::MoveAction;
}
Qt
::ItemFlags MyItemModel
::flags(const QModelIndex &index
) const{
QString type
= index.
data(FileTypeRole
).
toString();
// We should only be able to drop onto folders.
if (type != "File" && type != "ImageFile" && index.isValid())
return Qt::ItemIsDropEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled;
else
return Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled;
}
bool MyItemModel
::dropMimeData(const QMimeData *data, Qt
::DropAction action,
int row,
int column,
const QModelIndex &parent
) {
// this all works fine, so I'm cutting it for brevity.
}
{
// This is a list of the accepted mime types for dropping.
qstrList.append("text/uri-list");
return qstrList;
}
MyItemModel::MyItemModel(QWidget *topWidget, QObject *parent) :
QStandardItemModel(parent)
{
w = topWidget;
}
Qt::DropActions MyItemModel::supportedDropActions() const
{
return Qt::CopyAction | Qt::MoveAction;
}
Qt::ItemFlags MyItemModel::flags(const QModelIndex &index) const
{
QString type = index.data(FileTypeRole).toString();
// We should only be able to drop onto folders.
if (type != "File" && type != "ImageFile" && index.isValid())
return Qt::ItemIsDropEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled;
else
return Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled;
}
bool MyItemModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
{
// this all works fine, so I'm cutting it for brevity.
}
QStringList MyItemModel::mimeTypes () const
{
QStringList qstrList;
// This is a list of the accepted mime types for dropping.
qstrList.append("text/uri-list");
return qstrList;
}
To copy to clipboard, switch view to plain text mode
Everything works as far and drag and drop goes, but it makes it difficult to control where you are dropping with a visual indicator and without the tree expanding for you. Any tips on how to fix this would be greatly appreciated.
Bookmarks