PDA

View Full Version : QListView trouble accepting drops



thomaspu
6th September 2007, 20:26
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:

#include <QtGui>

class AdvQDirModel: public QDirModel
{
public:
AdvQDirModel(): QDirModel(){ }
~AdvQDirModel() { }

Qt::DropActions supportedDropActions() const
{ return Qt::CopyAction | Qt::MoveAction; }

};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

AdvQDirModel model;
QListView listView;
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

marcel
6th September 2007, 20:30
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

thomaspu
6th September 2007, 20:41
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

jpn
6th September 2007, 20:44
Set QDirModel::setReadOnly(false) and reimplement QDirModel::flags() to return Qt::ItemIsDropEnabled:


class AdvQDirModel: public QDirModel
{
public:
AdvQDirModel(): QDirModel() { setReadOnly(false); } // <---
~AdvQDirModel() { }

Qt::ItemFlags flags(const QModelIndex& index) const
{
Qt::ItemFlags f = QDirModel::flags(index);
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. :)


QDirModel model;
model.setReadOnly(false);
QListView listView;
...
listView.setModel(&model);

thomaspu
6th September 2007, 20:49
Ah, thanks Jpn. The flags is what I needed to get it working.

Paul