Effective Qt: Don’t be sub-class when subclassing

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 the Q_OBJECT macro, regardless of whether you also define signals or slots.
  • Prefer to offer all base class constructors in subclasses, too.
  • Prefer to offer the basic QObject constructor ( QObject * parent=0 ) when subclassing QObjects. Always offer the basic QWidget constructor ( QWidget * parent=0, Qt::WindowFlags f=0 ) when subclassing QWidget.
  • Prefer to follow the Qt constructor pattern: When inheriting QObjects, end constructor argument lists in QObject * parent=0. When inheriting QWidgets, end constructor argument lists in QWidget * 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.

About these ads

About marcmutz
Marc Mutz is a Senior Software Engineer, Trainer, and Consultant with KDAB.

3 Responses to Effective Qt: Don’t be sub-class when subclassing

  1. Anonymous says:

    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.

  2. Doe says:

    Very helpful, thanks. Just about the right level of complexity in your explanations.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: