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

(De-)serializing QObject tree #4

Closed
arietto opened this issue Mar 30, 2018 · 3 comments
Closed

(De-)serializing QObject tree #4

arietto opened this issue Mar 30, 2018 · 3 comments
Assignees
Labels

Comments

@arietto
Copy link

arietto commented Mar 30, 2018

How can QObject derived object with child objects be used with QtJsonSerializer? I mean that QObject already has children() getter. Is it possible to reuse embedded child-parent methods in collaboration with Q_PROPERTY instead of explicit adding m_children field?

@Skycoder42 Skycoder42 self-assigned this Mar 30, 2018
@Skycoder42
Copy link
Owner

Yes it is. A property can also be defined with explicit getters and setters. For your case that would be:

Q_PROPERTY(QList<QObject*> children READ children WRITE setChildren)

You will have to implement the setChildren yourself:

void setChildren(const QList<QObject*> &children) {
    for(auto child : children)
        child->setParent(this);
}

Please note that in order for this to work, all of the children your serialized object has must be polymorphic! Otherwise the childrens properties won't be serialized. See QJsonSerializer::polymorphing for more details.

@Skycoder42
Copy link
Owner

Was I able to answer your question? If yes please close the issue

@arietto
Copy link
Author

arietto commented Apr 10, 2018

Yes, thank you.

@arietto arietto closed this as completed Apr 10, 2018
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