PDA

View Full Version : QDataStream, QTreeWidgetItem, pointer



baca
15th February 2007, 09:29
At another QT forum I asked about stream and pointer: http://www.qtforum.org/thread.php?threadid=19550 and founded answer. Today I found this: http://www.qtcentre.org/forum/f-newbie-4/t-qtextostream-and-void-3247.html. Because I'm not sure I ask more precision:

If I want put into stream pointer to QTableWidgetItem I do this like that:

QTreeWidgetItem *item;
QDataStream stream_items;
int i;

(...)

// to stream
stream_items << reinterpret_cast<int> (item);

// from stream
stream_items >> i;
item = reinterpret_cast<QTreeWidgetItem *> (i);
My question: how check if the item readed and reinterpret from stream is already in memory (is not destroyed by another process in my program - my tree is dynamic playerlist and can be change by another playerlist)? "if (item)" is enough?

aamer4yu
15th February 2007, 09:37
You obviously cannot compare based on the addresses.
I gues u will be identifying the player with some unique value / property /characteristic.
What you will need is comparing the data within the class, for which u will need comparing operators / functions.

Subclass and provide your own rules for comparing the data if not provided by the base class.

baca
15th February 2007, 09:51
Hmm... I'm using only "top level item", so now I check if item are already in memory that:

if ((tree->indexOfTopLevelItem(item)) > -1)
// here I'm sure - item is in tree list and it is OK
It works, but I'm looking for simplest method :o

wysota
15th February 2007, 12:26
Honestly I don't see any reason why somebody would want to store pointers in a stream. What do you need it for? Maybe you could use some identifiers instead? Where does the stream lead to? Or maybe you could use a QList or QMap instead of the stream? You could then use QPointer (provided that your items inherit QObject - in your case you would have to subclass the item class)) to be sure your pointer is valid. Maybe if you explained what you're trying to do, we could suggest a better solution.

baca
15th February 2007, 14:31
I do drag&drop between three QTreeWidgets; each tree are diffrent columns:
1) I do "copy" of drag item if d&d is between diffrent trees,
2) I do "move" if d&d is inside the same tree.

After drop I check if dropped item(s) below to dropped tree:
1) if item not below I know that it's "copy" so I add new item to the tree,
2) another way I know it's "move" so swap items positions.

I don't need remember in d&d mime hole dragged items, but only pointer to them, because it can be that some process in my application remove one of dragged item from tree when I do d&d (between time when I drag and time when I drop dragged item). If I know pointer I can check before drop item if it's still in one of trees. That's I need pointer. I thought about QPointer but QTreeWidgetItem is not "a child" of QObject.

I think also about QList<QTreeWidgetItem *>, but I can't read it form stream:


QDataStream stream_items
QList<QTreeWidgetItem *> items

stream_items << items
stream_items >> items // can't be

wysota
16th February 2007, 13:03
Try reading to an int and then casting to a pointer. But I still suggest you use regular MIME.

baca
16th February 2007, 16:59
Thanks for answer!