Effective Qt: Don’t be sub-class when subclassing
2010-07-23 3 Comments
The first of my Effective Qt columns, “Don’t be sub-class when subclassing“, just went live on my site.
Here are the guidelines this item covers:
- When subclassing
QObject(directly or indirectly), always add theQ_OBJECTmacro, regardless of whether you also define signals or slots. - Prefer to offer all base class constructors in subclasses, too.
- Prefer to offer the basic
QObjectconstructor( QObject * parent=0 )when subclassingQObjects. Always offer the basicQWidgetconstructor( QWidget * parent=0, Qt::WindowFlags f=0 )when subclassingQWidget. - Prefer to follow the Qt constructor pattern: When inheriting
QObjects, end constructor argument lists inQObject * parent=0. When inheritingQWidgets, end constructor argument lists inQWidget * parent=0, Qt::WindowFlags f=0. Add additional arguments at the beginning of the argument list.
I hope to make this into a regular column, time permitting.

Good guidelines that I usually follow. The exception is not using Q_OBJECT macro, when the sub class is a template because moc does not support templates, unfortunately. Signal or slot template functions would be very useful for some things I’m doing now.
Thanks,
I’ve incorporated your comment into the article.
Very helpful, thanks. Just about the right level of complexity in your explanations.