PDA

View Full Version : Dropping into QListView



akiross
24th February 2008, 19:02
Hi there :)
First time using drag-n-drop so I'm posting here ;) Btw, qt 4.3.

I'm doing an application with a QListView wich contains some icons, this list acts like a "repository": these icons will be later used (dragged) in the application itself.
Anyway, I'd like to allow users to drag their image files from the desktop to the list view above.

I read the Model Subclassing Reference and the Using Drag and Drop with Item Views chapters (in addition to the Drag and drop one), ending up doing this:

creating a new model, subclassing QStandardItemModel
reimplementing
supportedDropActions()
flags()
mimeTypes()
dropMimeData()

Actually now I'm just trying to do something easy, like messageBoxing somewhere that mime data has been dropped. Later i'll do data processing.
So, just to see how drop works, the functions are these:


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

QStringList PieceModel::mimeTypes() const {
QStringList mimes;
mimes << "image/*";
return mimes;
}

Qt::ItemFlags PieceModel::flags(const QModelIndex &index) const {
Qt::ItemFlags defaultFlags = QStandardItemModel::flags(index);
return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
}

bool PieceModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex & parent) {
return true;
}


But actually the desktop manager (KDE in my case) won't allow me to drop something on there, even if


PieceModel *piecesModel = new PieceModel(this);
QListView *piecesView = new QListView(0);
piecesView->setModel(piecesModel);
piecesView->setDragEnabled(true);
piecesView->setAcceptDrops(true);
piecesView->setDropIndicatorShown(true);


What am I doing wrong?
Thanks :)

~Aki

wysota
24th February 2008, 19:22
I don't think a mime of "image/*" will work - if you drag a file from the desktop it has a text/uri-list mime type. And I suggest checking some other model than the standard item model, because this model is really weird and it probably ignores your flags() implementation (and don't ask me why). Check if it's called at all...

akiross
24th February 2008, 22:10
Yeah, you were right :)
It didn't come in my mind to check if these functions were called...
Anyway I reimplemented QAbstractListModel and it calls the drop function correctly now :)

Thanks!