Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why declare a slot as a slot?

Tags:

c++

qt

If basically anything can be used as a slot for a signal in Qt (provided that the signatures are compatible), why would I ever want to declare a member function as a slot? Is declaring a member function as a slot really just a hold over from the pre-C++11 days?

like image 347
Arcadio Alivio Sincero Avatar asked Oct 17 '25 01:10

Arcadio Alivio Sincero


1 Answers

It has indeed become a rare situation but there are still some uses to declaring methods as slots, if you want to use the SIGNAL()/SLOT() macros in a connection (aka string-based connection). For example:

  • If you use e.g. a factory to create instances of several unrelated classes, you can create a string-based connection without doing introspection on the involved object(s).
    A good example of that is QLineEdit and QTextEdit; they do not inherit one another but both have a textChanged signal and you could make a window where instances of the former or the latter are added dynamically to it.
  • If you want to make a signal to slot connection where the slot has more arguments (with default values) than the signal.
    This is explained here.
  • For connecting C++ Objects to QML Objects (from the same page), as signals/slots of QML objects are simply text in a .qml file.

The cons of string-based connections (or I guess pros of functor-based connections) are listed in the same page.

like image 167
Atmo Avatar answered Oct 18 '25 15:10

Atmo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!