S
S
sddvxd2018-09-15 21:53:26
Qt
sddvxd, 2018-09-15 21:53:26

How to get the bytes of a class instance?

Hello!
It was necessary to write the binary value of the class instance into the byte stream. I wanted to implement similar to the overloaded << for QDataStream:

QByteArray arrBlock;
QDataStream binaryStream(&arrBlock, QIODevice::WriteOnly);
binaryStream << QPixmap();

Please tell me how can I implement this with my class (get the binary information of the instance)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ighor July, 2019-11-26
@IGHOR

Here is the answer to the question, but it does not apply to QPixmap , QPixmap::save should be used there..

#pragma pack(push, 1)
struct MyStruct
{
qint64 varA;
qint64 varB;
};
#pragma pack(pop)

MyStruct myStruct;
myStruct.varA = 234;
myStruct.varB = 4534;

QByteArray structByteArray(reinterpret_cast<const char*>(&myStruct), sizeof(MyStruct));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question