Skip to content
This repository has been archived by the owner on Mar 4, 2023. It is now read-only.

Deserialize QObject* with parametrized constructor #22

Closed
milad-gh opened this issue Aug 29, 2019 · 6 comments
Closed

Deserialize QObject* with parametrized constructor #22

milad-gh opened this issue Aug 29, 2019 · 6 comments
Assignees
Labels

Comments

@milad-gh
Copy link

Hello,
Is it technically possible to have Q_INVOKABLE constructor that takes some parameters when deserializing a QObject ??

@Skycoder42 Skycoder42 self-assigned this Aug 29, 2019
@Skycoder42
Copy link
Owner

Not really, no. How would the serializer know what these parameters are? Do you want to deserialize some json properties as arguments for the constructor?

More details on your use case might help. If you have a class you cannot modify, I would recommend you to create a wrapper class that takes care of this.

@milad-gh
Copy link
Author

Thanks for your reply,
The main issue is that i want to serialize/deserialize some QObject derived classes but this classes are logically dependent to each other, e.g class2 has a constructor that takes class1 as argument. and i want to serialize these two classes.
For serialization everything is OK but for deserialization after i deserialized class1, i want to pass it to class2 constructor when i'm constructing (desalinizing) class2.

Now i'm using a wrapper class as you mentioned, to handle these dependencies, but its a little bug prone because classes should have a setter methods rather than constructors.
i was wondering maybe its possible to have a none-default constructors for QObjects.

@Skycoder42
Copy link
Owner

Skycoder42 commented Aug 31, 2019

It is not possible with the current architecture. However, you can create your own QJsonTypeConverter to handle this situation.

Just create one by copying the code from QJsonObjectConverter and then adjust it to work the way you need it to. Just make sure to give it at least a High priority, so it is preferred to be used over the standard QObject converter.

@milad-gh
Copy link
Author

Thanks for your help,
I'll try to use this approach.

@Skycoder42
Copy link
Owner

Any success yet? If yes, you can close the issue. If not, I might be able to give further help...

@milad-gh
Copy link
Author

Actually i tried to redesign some part of my code without using QJsonObjectConverter, because i wanted to keep source code simple for now.
But it seems using QJsonObjectConverter is a robust approach for future.
I appreciate your help.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants