QByteArray to QObject typecasting
-
It seemed a trivial problem but somehow I did not find an example of casting from QByteArray to a QObject. I have tried several ways like QObject *temp = qobject_cast<QObject *>(QByteArray) .
@ddze
Well, you can't! These classes are not in the same hierarchy, so neitherqobject_cast, nordynamic_castwill work. In factQByteArrayis 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 ?
-
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 copyQObjectinstances. If you have some data put into aQObjectone 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 aQObjectinstance? 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) aQObjectinstance.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; -
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.