Sometimes you just have to know the function (or at least know that it exists) to spot it in the documentation.
Sometimes you just have to know the function (or at least know that it exists) to spot it in the documentation.
Thanks again for the tips about the two functions, they did the trick! I wonder if there maybe could be some better way to document the functions, like in subcategories. Because it's not only in the Qt documentation this is a problem. It's the same way in Java.
The only thing left for my view class is to make the actual drag and drop functionality. But again I'm uncertain of how I should do it. Suprised?
The thing is that all examples and threads I can find is about drag n dropping items between widgets. I don't want my view to let items from other widgets be dropped on it, or let it drop it's items on other widget.
The point with my view is to be a kind of a Outliner, like in Maya, where the user can organize the scene of objects into groups. The user should be able to make a object to become a member of a group by dropping it on that group.
The model is actually a vector of pointers to the components, which can be groups of other components, in my scene. The vector has index (0,0) in the model and is acting as the root.
My question is:
Is the simplest approach to just get the target information from the drag and drop and do the actual grouping in my scene completely on my own and then rebuild my model? In this case there wouldn't be any actual dragging and dropping in normals sense... Or should I make the grouping by implement a modelItem class that handles the grouping?
/pir
Drag and drop is mostly handled by the model and not by the view.
These methods (of the model) may be of interest for you:
bool dropMimeData ( const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent )
QMimeData * mimeData ( const QModelIndexList & indexes ) const
QStringList mimeTypes () const
Qt::DropActions supportedDropActions () const
pir (21st May 2006)
ok... stuck again.
I have problem with passing the information that is dragged... I've tried QAbstractModel::mimeData(), do I need to reimplement it or is it just ok to pass the QModelIndex into it?
Then I have a big problem with retrieving the QModelIndex back from the QMimeData.... how am I supposed to do that? There is a function called QMimeData::retrieveData(), can I use that one as it is or do I reimplement it? It is protected, which I find kind of weird if it supposed to be used in this way...
ahhhh, I'm tired of this drag n dropping stuff...
/pir
You should not pass indexes there. You need to encode your data as some custom mime-type (for example application/x-my-mime-type). If you only drag within the same widget, you can for example use the index row number as the encoded form of data (as long as you're able to recreate an item from it). The easiest way is something like this:Originally Posted by pir
Qt Code:
QString rows; } mime->setText(rows); return mime; }To copy to clipboard, switch view to plain text mode
In dropMimeData just reconstruct indices from their row values and you're done. Of course you may choose to do it more properly and use QByteArray and a custom mime-type instead of text/plain (which is used when using setText()). Or you could even do it even more properly and encode not the index of the item but its data (as should be done with "real" drag&drop).
This fit my purposes best, I think. The only thing my indexes containt is a pointer to a component in my scene. So it should be sufficuent to give this pointer as information... this was actually my first approach but I couldn't find how I was supposted to convert the pointer to a QByteArray and back.Originally Posted by wysota
But there is one problem with this approach. I need to be able to remove the old index when the user have dropped the object, since it should be executed as a move action. So I need to find that index, and then I suppose I need to know its row and parent, and the parent's row and parent etc... maybe it's simpler to have a pointer in my model class pointing to the index being dragged and use this information instead of a mime. Could this create problems somehow?
/pir
Last edited by pir; 22nd May 2006 at 08:27.
A pointer is an integer, just treat it as such. But if you want to encode the data itself, then I didn't mean a pointer here, but actual information which is stored in an object referenced by the pointer.Originally Posted by pir
I guess the view might do it yourself if you convince it you are doing a move action (but I'm not sure, haven't tried it myself yet).But there is one problem with this approach. I need to be able to remove the old index when the user have dropped the object, since it should be executed as a move action.
That's why the concept of indexes existsSo I need to find that index, and then I suppose I need to know its row and parent, and the parent's row and parent etc...But if you implement the drop correctly, you won't have to worry about removing the item from its original place.
This doesn't give you any advantage. You can store the pointer in the QMimeData object as well.maybe it's simpler to have a pointer in my model class pointing to the index being dragged and use this information instead of a mime. Could this create problems somehow?
pir (22nd May 2006)
What may help you a bit is to look at my source code here : http://code.google.com/p/knewz/source/browse/trunk
I ran into this the other day, it's a bit of work to get right but not terribly complicated once you get the hang of it. There's a few things to remember while dragging and dropping stuff, but you should be able to pick them up from my code if you haven't already.
There pertinant stuff you will want to look at is the model and the mimedata implementations:
http://code.google.com/p/knewz/sourc...knewzmodel.cpp
http://code.google.com/p/knewz/sourc...zbmimedata.cpp
Like you, I am just passing pointers to objects around in a QList. So with a drag and drop you just dump the pointer list into a custom mimedata class and retrieve it when it is dropped. Then you can use your underlying data structure to manipulate the data directly, encapsulating any removal and insertion of rows with the correct calls to being/endRemoveRows and begin/endInsertRows
Bookmarks