QListView trouble accepting drops
I was trying to make a simple example to have a QListView accept a dropped file from the OS and can't seem to get the thing to work. I was reading about it in the Qt Assistant article "Using Drag and Drop with Item Views" and I understand that the view and the model need to be able to accept drops. I'm pretty sure that the view properties are set correctly, but I'm not sure about the model. What am I missing?
Here's the complete code:
Code:
#include <QtGui>
{
public:
~AdvQDirModel() { }
Qt::DropActions supportedDropActions() const
{ return Qt::CopyAction | Qt::MoveAction; }
};
int main(int argc, char *argv[])
{
AdvQDirModel model;
listView.setAcceptDrops(true);
listView.setDropIndicatorShown(true);
listView.setModel(&model);
listView.
setWindowTitle(QObject::tr("Dir View"));
listView.resize(320, 240);
listView.show();
return app.exec();
}
Thanks,
Paul
Re: QListView trouble accepting drops
QDirModel has a reimplemented version of supportedDropActions from QAbstractItemModel..
If your model is derived from it or is a subclass of QAbstractItemModel, then you might
want to reimplement it too.
Also, take a look at QAbstractItemModel::dropMimeData.
Regards
Re: QListView trouble accepting drops
As my example shows, I'm reimplementing supportedDropActions, so I don't think thats the problem.
I had started to write a simple dropMimeData for the new model -> AdvQDirModel, but when I set a breakpoint in my dropMimeData, run the app and drag a file from my desktop to it, it never enters the function. So I don't think that isn't the problem either.
Paul
Re: QListView trouble accepting drops
Set QDirModel::setReadOnly(false) and reimplement QDirModel::flags() to return Qt::ItemIsDropEnabled:
Code:
{
public:
AdvQDirModel
(): QDirModel() { setReadOnly
(false);
} // <--- ~AdvQDirModel() { }
Qt::ItemFlags flags(const QModelIndex& index) const
{
if (isDir(index))
f |= Qt::ItemIsDropEnabled; // <---
return f;
}
};
EDIT: Actually, now that I relook at it, all you need is to set QDirModel::setReadOnly(false) and that's all. You don't even need to reimplement flags(). The default implementation will enable dropping once read only property is set to false and the dropping target is a writable directory. :)
Code:
model.setReadOnly(false);
...
listView.setModel(&model);
Re: QListView trouble accepting drops
Ah, thanks Jpn. The flags is what I needed to get it working.
Paul