PDA

View Full Version : Drag and Drop



starcontrol
28th April 2008, 14:36
Hi,
i try to implement DragAndDrop Suuport in my simple calender application. It is model/view based, so I read the trolltechs docu:
http://doc.trolltech.com/4.3/model-view-dnd.html

It is not clear for me how I have to proceed.
My application is a table view which works like the outlook calender. You have events in the calender, these events have a start date, end date, event number and two booleans.
So i like to drag and drop these events to a different date in my table.
Do you know, what I have to implement to reach this ?

What do Ihave to do with the method:
QStringList DragDropListModel::mimeTypes() const

and what do I have to do with the method:
QMimeData *DragDropListModel::mimeData(const QModelIndexList &indexes) const

?

Thanks,
Alex

wysota
28th April 2008, 16:38
mimeTypes() should return a list of mime-types your model can generate and handle. You'll be using a custom type, so it should probably be something like application/x-calendar-event (you can put whatever you want after the "x-" part, just stick to the same name everywhere).

mimeData() should encode (serialize) the object you want to drag (given by the list of model indexes) into a byte array which can then be transferred to another object and decoded using dropMimeData() or another drop mechanism (dropEvent() in case of Qt apps).

yuriry
26th September 2008, 19:31
What would be the best way to extract QModelIndexList from QMimeData inside dropMimeData()? I call data->data("application/x-qabstractitemmodeldatalist") to get an instance of QByteArray, but am not sure how to proceed from this point forward :(

I tried the following:



QByteArray buf = data->data("application/x-qabstractitemmodeldatalist");
QDataStream s(&buf, QIODevice::ReadOnly);
QModelIndexList list;
s >> list;


but the compilation fails because there is no operator>>(QDataStream& s, QModelIndex& idx). I could implement the operator but I do not know how QModelIndex is encoded in QByteArray.

caduel
26th September 2008, 19:49
A list of QModelIndexes makes only sense within an application (at least, I can't think of another scenario right now).
Within an application you can access the QMimeData object you created at the drop site.
So you can just create a subclass of QMimeData in mimeData(). This subclass could feature a QList<QModelIndex> as a member variable.
Use qobject_cast to case to that subclass and just get the QList<QModelIndex> without worrying about how to store those as binary mime data.

HTH

yuriry
26th September 2008, 20:12
Thanks, Caduel. I also noticed that I missed the point of mimeData(): it does not serialize model indexes but, instead, the items of data corresponding to those indexes.