PDA

View Full Version : QTextOStream and void*



mclark
4th August 2006, 22:22
I need to extract a void* from a QTextOStream.


QDataStream dataStream;
void* pData;

dataStream >> pData;

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!

jacek
5th August 2006, 12:01
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.

mclark
7th August 2006, 15:29
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.

jacek
7th August 2006, 16:13
I've also tried the integer casting but have not been able to make that work.
Something like this should work:
Q_ASSERT( sizeof( quint32 ) == sizeof( void * ) );
stream << reinterpret_cast< quint32 >( ptr );
...
quint32 v;
stream >> v;
ptr = reinterpret_cast< void * >( v );(if you have 64-bit system, you should use quint64).


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

mclark
7th August 2006, 18:40
Thanks for the tips. It's now working using the casting you suggested.