Results 1 to 5 of 5

Thread: QTextOStream and void*

  1. #1
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QTextOStream and void*

    I need to extract a void* from a QTextOStream.

    Qt Code:
    1. QDataStream dataStream;
    2. void* pData;
    3.  
    4. dataStream >> pData;
    To copy to clipboard, switch view to plain text mode 

    The QTextOStream documentation states that an operator, << (const void*), exists but no >> (void*) operator is listed. Using the << operator, I can get the void* into the stream but can't get it out. What must I do to get my void* out of the stream?

    Thank you!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextOStream and void*

    Well... you could read it into an integer and do reinterpret_cast, but are you sure that this pointer will be still valid?

    PS. Note that QTextOStream is a part of Qt3 Support module.

  3. #3
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextOStream and void*

    The pointers will be valid, but I was unaware of the Qt3 support mode. I've also tried the integer casting but have not been able to make that work.

    I'm trying to move info from a table row from one table to another via drag and drop. The code I'm working with copies all table elements using the QTestOStream. I've added the pointer as a data value to one of the table cells.

    Is there another stream class that would be better suited for handling both text and data?

    As an alternative I could store the dragged info and reclaim it on the drop. I would rather not disrupt the existing code to do this, which is why I am trying to add another << / >> to the existing stream.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextOStream and void*

    Quote Originally Posted by mclark
    I've also tried the integer casting but have not been able to make that work.
    Something like this should work:
    Qt Code:
    1. Q_ASSERT( sizeof( quint32 ) == sizeof( void * ) );
    2. stream << reinterpret_cast< quint32 >( ptr );
    3. ...
    4. quint32 v;
    5. stream >> v;
    6. ptr = reinterpret_cast< void * >( v );
    To copy to clipboard, switch view to plain text mode 
    (if you have 64-bit system, you should use quint64).

    Quote Originally Posted by mclark
    Is there another stream class that would be better suited for handling both text and data?
    You can try QDataStream.

  5. #5
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Smile Re: QTextOStream and void*

    Thanks for the tips. It's now working using the casting you suggested.

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
  •  
Qt is a trademark of The Qt Company.