[Qt-interest] Drag and Drop Pointers/QModelIndex Within Application
Karol Krizka
kkrizka at gmail.com
Mon May 25 14:46:19 CEST 2009
On Mon, May 25, 2009 at 1:57 PM, Karol Krizka <kkrizka at gmail.com> wrote:
> On Mon, 2009-05-25 at 12:25 +0200, Robert Hairgrove wrote:
>> Karol Krizka wrote:
>> > Hi there,
>> >
>> > I'm basically trying to add a DnD interface to my application. I do not
>> > need to be able to drag/drop outside of the application.
>> >
>> > I basically have a list of categories, each with a set of items
>> > organizes in a tree (QTreeView). I want to be able to move the items
>> > between the categories by dragging and dropping them. I thought the best
>> > idea would be to pass the QModelIndex of the items being dragged in the
>> > DnD event and then in the dropAction, I would figure out where the index
>> > came from and where it was dropped, then perform the move. Another idea
>> > was that I pass the pointer to the item.
>> >
>> > However, the data for a DnD event seems to be passed via the QMimeData
>> > structure, which only supports data in the format of color, string, html
>> > or a bytestream.
>> >
>> > So, what is the best way to pass data using DnD inside an application?
>> >
>> > --
>> > Cheers,
>> > Karol Krizka
>> > http://www.krizka.net
>>
>> You can register your own class as a new mime data type, but you must
>> implement the streaming operators for it (i.e., << and >>). It is very
>> easy to do, though.
>>
> Yes, I think I will have to do that.
>
I started to implement my own QMimeData class, as suggested at the end
of the QMimeData documentation. The documentation also says that I
need to reimplement "hasFormat(), formats(), and retrieveData()".
While poking around the source code for QMimeData, to see how the
function were implemented there, I found out that all data is stored
as a QVariant, but all the functions for that are private. I know how
to store a pointer to a QObject inside a QVariant... So, my new
question is, is there a reason why I cannot directly store my data as
a QVariant using the stock QMimeData class? Or even using a custom
derivative (which I'm working on) of the QMimeData class?
>> Open QtAssistant and read the "Drag and Drop" overview as well as the
>> description of the QMimeData and QDropEvent classes. There are many
>> examples there.
> I did look at those examples and the documentation. However, either of
> them demonstrate what I want to do. They either pass around simple types
> (ei: strings, images) or work with files outside of the application (ei:
> images via QByteArray).
>
> Thank you for your reply!
--
Cheers,
Karol Krizka
http://www.krizka.net
More information about the Qt-interest-old
mailing list