[quote]What is the charm pattern?[/quote]
Perhaps that indeeds needs a bit of explanation. In the past, there have been examples of classes that added certain behaviour to a widget. A good example is the "FlickCharm":http://labs.qt.nokia.com/2008/11/15/flick-list-or-kinetic-scrolling/ that demonstrates adding kinetic scrolling to scroll area's. The pattern shown there can be applied much more broadly. There are many other things you can achieve in much the same way.
The basic idea is that you create a class, typically a QObject-derived class, that "magically" adds functionality to another object. That is: not through subclassing, but by instantiating a new object that manipulates how the widget looks or works. You can use things like event filters to make them work. I write them in a way that they manage their own life time, by making themselves children of the object they are supposed to act on. Futhermore, I usually make the constructor private, and create a static method like this:
@static MyCharm* MyCharm::cast(QWidget* subject) ;
that you use to create the charm. That allows tricks like making sure that there is only one instance of the charm in question for a single subject widget.
Working this way is quite a powerful programming pattern. It allows you to change the behavior in lots of little but very useful ways, without subclassing. The charm can often apply to multiple types of widgets, and it prevents an explosion of derived types.
Examples of charms I have written, are:
- A charm to enable verifying of the contents of tabs before allowing a tab change in QTabBar or QTabWidget
- A charm to make it possible to rename tabs of QTabBar or QTabWidget
- A charm to add small inline command buttons inside or outside a QLineEdit or a QComboBox. You can cast this charm multiple times to add multiple buttons.
- A charm to make the selection list of a QComboBox wrap around.
For this way of programming, Qt offers some very powerful tools to make this possible, especially the event filter. I call this the charm pattern.