QByteArray to QObject typecasting
-
@ddze
Well, you can't! These classes are not in the same hierarchy, so neitherqobject_cast
, nordynamic_cast
will work. In factQByteArray
is a base class, and doesn't depend on the Qt's meta-object system in comparison toQObject
.Kind regards.
-
Hi and welcome to devnet,
In addition to what @kshegunov wrote. What are you trying to serialize exactly ?
-
What would you like to do with that object ?
-
@ddze
Usually you don't do that, for the same reason that you don't copyQObject
instances. If you have some data put into aQObject
one usually extracts that data by a method and serializes the data itself, not theQObject
. Consider what you're supposed to do to serialize the object hierarchy (with the parents and children) for aQObject
instance? Also signals, slots and connections can't be trivially expanded in means of serialization ... there are tons of problems that prevent you from actually copying/serializing (which is basically the same thing) aQObject
instance.Kind regards.
-
Your class must implement this operators:
QDataStream &operator<< QDataStream &operator>>
After that you can use this code:
MyClass c; QDataStream reader(byteArray); reader << c;
-
I know that and it would be easy if there was no a pointer to a QGraphicItem in the object structure (which has no << , >> ) operators.
Also , my class provides the insertion and retraction operators but seems impossible to use them in the MIme as they fail to populate the members of the class such as QByteArray , QGraphicItem ... since these classes do not provide <<>> operators on their own.
Looks pretty limited staff that can be transferred with the QMimeData , basic texts, urls, qt-class objects , but the objects from non Qt classes containing the Qt class members with no << >> operators, it is hard or even impossible - since it is requirement that they have << >> in order to use the QDataStream object.