I have 3 components, one of which is parent of the other 2. Say they are Parent and Child0 and Child1. All the 3 components can emit signal changed(). When one of the child emit changed(), Parent need to emit changed() and the other child need to call slot onChanged()
My question is, which would be better in the view of design?
design 1: connect signals of each child to the Parent's signal and slot of the other child.
connect(Child0, SIGNAL(changed()), Child1, SLOT(onChanged()));
connect(Child1, SIGNAL(changed()), Child0, SLOT(onChanged()));
connect(Child0, SIGNAL(changed()), this, SIGNAL(changed())); // this refers to Parent
connect(Child1, SIGNAL(changed()), this, SIGNAL(changed()));
connect(Child0, SIGNAL(changed()), Child1, SLOT(onChanged()));
connect(Child1, SIGNAL(changed()), Child0, SLOT(onChanged()));
connect(Child0, SIGNAL(changed()), this, SIGNAL(changed())); // this refers to Parent
connect(Child1, SIGNAL(changed()), this, SIGNAL(changed()));
To copy to clipboard, switch view to plain text mode
design 2: connect signals of each child to a special slot, in which Parent will emit signal changed() and the other child call onChanged()
connect(Child0, SIGNAL(changed()), this, SLOT(onChildChanged()));
connect(Child1, SIGNAL(changed()), this, SLOT(onChildChanged()));
...
onChildChanged(){
if(sender() == Child0)
{
Child1->onChanged();
}
else if (sender() == Child1)
{
Child0->onChanged();
}
emit changed(); // this refer to Parent
}
connect(Child0, SIGNAL(changed()), this, SLOT(onChildChanged()));
connect(Child1, SIGNAL(changed()), this, SLOT(onChildChanged()));
...
onChildChanged(){
if(sender() == Child0)
{
Child1->onChanged();
}
else if (sender() == Child1)
{
Child0->onChanged();
}
emit changed(); // this refer to Parent
}
To copy to clipboard, switch view to plain text mode
Bookmarks