Thursday, March 22, 2012

[QT] Signal & Slot

Recently because of the need at work for developing Maya plugin, I got a chance to dabble in Qt. Always excited about new things to learn!! :)

As the central feature of Qt, the first thing I started experimenting is the Signal & Slot mechanism. I found that there are some similarity  between Qt's Signal & Slot and Maya's Push & Pull model. Probably this is the reason why Maya chose Qt to rewrite the entire GUI from 2011 (just guessing :p)

Instead of using traditional callback functions, Qt adopts an alternative approach: Signal & Slot. When an event occurs (or object's internal state changed), it emit signals, objects interested in this event would use slot to respond to the signal. It doesn't know if anything receives the signals it emits; and the slot itself doesn't know if any signals connected to it. Loose Coupling!! Oh... and the signature of signal should match the signature of slot.

QObject::connect(&a, SIGNAL(valueChanged(int)), &b, SLOT(setValue(int)));

Don't you think the usage of Signal & Slot is kind of like the following in Maya when trying to build dependent relationship between attributes?

attributeAffects(width, area);
attributeAffects(height, area);

In Maya, each node just knows about itself, it doesn't know anything about node connection, not knowing which node connects to it and which node it connects to. It only knows which input attribute affects which output attribute of itself. It's just like a black box with a compute function inside to do the necessay calculation. With commands, we can connect nodes together. With Maya's Push & Pull model, we can get the most updated state of a particular node.

That's why I think that there are some common ideas between these two architectures. They all maintain truly independent components and provide ways to bind objects(nodes) together for communication.

No comments:

Post a Comment