Access to shared map between parent - children
Hi,
In my applications I have class which contains QMap, and several child classes which execute tasks on the variables from this map.
Now I would ask you, which way for communication between parent - child is a better in this case:
- cast the parent pointer to the right type and then call parent function
- store the pointer to the parent somewhere in the child when receiving it in the constructor
or use signals and slots - send signal to parent with key as argument and then wait for value from map?
But with signals and slots is a problem, because when child send signal with key to parent class then parent should send signal with value to only specific child object but signal is send to all child.
Which way you choose when you have a similar problem?
Thanks,
Re: Access to shared map between parent - children
Either keep a pointer to the parent or share the map.
Cheers,
_
Re: Access to shared map between parent - children
I can not share the map because it must be protected from simultaneously access at the same time something like this
Code:
{
//.......
return mMap.value(key);
}
Quote:
Either keep a pointer to the parent or share the map.
Do you mean keep pointer inside class and initialize it inside constructor?
Code:
class Child {
};
class Parent {
public:
Parent(Child * child) : mChild(child) {};
private:
Child * mChild;
};
or use something like that
Code:
public:
Parent() {
child = new Child(this);
}
private:
Child *child;
};
public:
Parent* parent = qobject_cast<Parent*>(this->parent());
parent->accessToMap("somekey");
}
};
Re: Access to shared map between parent - children
Ah, you didn't say the the children are used by different threads, nor that the map is modified during runtime.
You could still pass a synchronized container (e.g. wrapper around the map) but option 1 (explicitly typed member) looks good as well.
Cheers,
_
Re: Access to shared map between parent - children
But it looks like that, I store duplicate a pointer to parent, because QObject already has it and return it via parent() method. I am right?
Whether first method is better in multithreading app from that
Code:
Parent* parent = qobject_cast<Parent*>(this->parent());
parent->accessToMap("somekey");
or it is the same?
Thanks,
Re: Access to shared map between parent - children
Sure, if you want to lose the type safety or incure the runtime overhead of a checked cast, that's your choice.
Cheers,
_