private slots

Simon Hausmann hausmann at
Wed Jan 3 15:05:49 GMT 2007


while we're on the topic of public APIs and kdelibs cleanups: Here's a neat 
little moc trick that we use in Qt 4 and that we can very well also use in 

It is very common to need slots in public classes. Take KDateWidget for 

protected Q_SLOTS:
  void slotDateChanged();
    KDateWidgetPrivate *d;

This slot is not intended for applications to call/use and it is connected to 
the internal widgets. There's no need to have it in the public API and 
exported. You can use the following instead:

    Q_PRIVATE_SLOT(d, void slotDateChanged())

    KDateWidgetPrivate *d;

This macro expands to nothing but moc reads it nevertheless. It will generate 
a slot with the given signature in the metaobject, so you can connect to it. 
But it assumes that the method itself is a member function of the first 
argument of the macro. So the generated code will look like

... [deep in the metacall ]

If that moc generated code is included from within the .cpp file (like we 
fortunately do most of the time in KDE) d is well-defined and the compiler 
can call your private slot, which is now just a member function of your d 

And of course in general I suggest not to have any private methods in our 
public API at all. Putting it into your d-pointer gives you more flexibility 
in the future to change the implementation. There is really no good reason 
for exporting private functions.

I've heard gcc 4.2 will apply the default (hidden) visibility to private 
members, so they won't exported anymore. But I hope that at some point in the 
future we can provide binary compatibility in kdelibs with more than just gcc 
> 4.2 :)

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <>

More information about the kde-core-devel mailing list