[Interest] Are slots even needed these days?

Gian Maxera gmaxera at gmail.com
Wed Mar 16 15:31:33 CET 2016


> On 16 Mar 2016, at 14:24, Nikos Chantziaras <realnc at gmail.com> wrote:
> 
> Since in modern Qt you connect signals to functions/lambdas, is there a reason to declare slots anymore?
> 
> In other words, is there any difference between:
> 
>  class Foo: public QObject {
>      Q_OBJECT
> 
>  public slots:
>      void bar();
>  };
> 
> and:
> 
>  class Foo: public QObject {
>      Q_OBJECT
>  public:
> 
>      void bar();
>  };
> 
> I can connect to Foo::bar either way. If I don't intend to ever use the old-style connect syntax, is there a reason to have "public slots:" anymore?

One reason that for me it’s fundamental: Readability of your code !!!

Even if it wouldn’t necessary to declare “public slots”, I really strong advice to use “slots” because it explicitly say what part of your interface are supposed to be attached to a signal and what part not.

It’s very common on my C++ code that only a subset of method are mean to be attached to a signal, so make this distinction clear help to understand the code and debug it more easily.

Then, I don’t know if there is a real difference on connecting to a function or to a explicitly declared slot. But for sure, you cannot use always lambdas. So methods acting as slot are necessary.

Ciao,
Gianluca.




More information about the Interest mailing list