PDA

View Full Version : setDropIndicatorShown and setAutoExpandDelay not working in subclassed QTreeView



ImperialPenguin
18th August 2012, 15:24
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) :
QTreeView(parent)
{
setSelectionMode(QAbstractItemView::SingleSelectio n);
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();
}


I don't know if this code matters in this case, but this is my subclassed standard item model:


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;
}


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.

ImperialPenguin
20th August 2012, 18:49
I should add that everything worked until recently. I don't know what broke it, but I did upgrade to Qt 4.8 in that time frame. When it was working before, I didn't have the calls to dragEnterEvent and dragMoveEvent in there. Now, if I don't have them, drag and drop doesn't work at all. Could they be blocking access to the drag data from the model? The model accepts the drop okay, but I don't know if it sees anything that is going on before that happens. I'm not sure why it worked without those calls before and now it doesn't.

norobro
21st August 2012, 01:00
If that is all of your tree view code, why subclass QTreeView?

Take a look at the source code of QAbstractItemView::dragMoveEvent() (here (https://qt.gitorious.org/qt/qt/blobs/4.8/src/gui/itemviews/qabstractitemview.cpp#line1883)) to see what you are overriding.

ImperialPenguin
21st August 2012, 04:04
I subclassed the treeview because drag and drop doesn't work at all without the dragenterevent and dragmoveevent. My code looked exactly like the code in the item puzzle drag and drop from what I could tell, but it didn't work for me until I subclassed and added those two functions.

norobro
21st August 2012, 05:30
Try forwarding the QDragMoveEvent to the base class:
void MyTreeView::dragMoveEvent(QDragMoveEvent *event)
{
event->acceptProposedAction();
QTreeView::dragMoveEvent(event);
}

ImperialPenguin
21st August 2012, 06:14
That doesn't work either, drag and drop quits working completely if I do that. Drag and drop functionality comes back if I accept the action after forwarding the event, but it still doesn't autoexpand or highlight correctly.
I really don't understand why it worked fine a month or so ago and not now. As far as I know, I haven't editing the two classes involved at all since then. And I don't think upgrading Qt would break it. It's certainly frustrating though.

NeoCode
22nd August 2012, 20:13
I wanted to disable auto-expansion tree nodes during darg&drop. Calling setAutoExpandDelay(-1) in constructor of subclassed tree not provide the desired result.
I called setAutoExpandDelay(-1) in dragEnterEvent() - it helped.