Results 1 to 7 of 7

Thread: QDataStream, QTreeWidgetItem, pointer

  1. #1
    Join Date
    Feb 2007
    Location
    Poznan, Poland
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QDataStream, QTreeWidgetItem, pointer

    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-newb...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:
    Qt Code:
    1. QDataStream stream_items;
    2. int i;
    3.  
    4. (...)
    5.  
    6. // to stream
    7. stream_items << reinterpret_cast<int> (item);
    8.  
    9. // from stream
    10. stream_items >> i;
    11. item = reinterpret_cast<QTreeWidgetItem *> (i);
    To copy to clipboard, switch view to plain text mode 
    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?

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDataStream, QTreeWidgetItem, pointer

    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.

  3. #3
    Join Date
    Feb 2007
    Location
    Poznan, Poland
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDataStream, QTreeWidgetItem, pointer

    Hmm... I'm using only "top level item", so now I check if item are already in memory that:
    Qt Code:
    1. if ((tree->indexOfTopLevelItem(item)) > -1)
    2. // here I'm sure - item is in tree list and it is OK
    To copy to clipboard, switch view to plain text mode 
    It works, but I'm looking for simplest method

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDataStream, QTreeWidgetItem, pointer

    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.

  5. #5
    Join Date
    Feb 2007
    Location
    Poznan, Poland
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDataStream, QTreeWidgetItem, pointer

    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:

    Qt Code:
    1. QDataStream stream_items
    2. QList<QTreeWidgetItem *> items
    3.  
    4. stream_items << items
    5. stream_items >> items // can't be
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDataStream, QTreeWidgetItem, pointer

    Try reading to an int and then casting to a pointer. But I still suggest you use regular MIME.

  7. #7
    Join Date
    Feb 2007
    Location
    Poznan, Poland
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDataStream, QTreeWidgetItem, pointer

    Thanks for answer!

Similar Threads

  1. QDataStream, QTreeWidgetItem, empty list
    By baca in forum Qt Programming
    Replies: 2
    Last Post: 15th February 2007, 14:43
  2. QTreeWidgetItem ?
    By allensr in forum Qt Programming
    Replies: 5
    Last Post: 3rd January 2007, 17:51
  3. QTreeWidgetItem swap or move up one level problem
    By patrik08 in forum Qt Programming
    Replies: 2
    Last Post: 24th September 2006, 18:34
  4. simple pointer question
    By mickey in forum General Programming
    Replies: 6
    Last Post: 16th June 2006, 09:19
  5. QDataStream >> QString
    By smalls in forum Qt Programming
    Replies: 2
    Last Post: 17th January 2006, 22:14

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.