General question about Signals/Slots compared to lamda functor passing



  • Hi all,

    I have completed my first Qt application. I have previously been used to pass functors to other classes to create a form of callback functionality, which has worked pretty well.
    With Qt I'm of course using the Signals and slots, and since I'm used to lambas I felt it was a welcome addition as of 5.5 to add those to the slots functionality.

    However, I am confused as to what I gain compared to just using lamdas as callback function?

    I know that signals/slots are safe aross threads, so thats one advantage compared to functors.

    As the documentation states, classes that don't know anything about each other can connect to eachothers signals/slots but is that true? You still need both objects to create the connection, right?

    So please enlighten me, I'm baffled :)


  • Lifetime Qt Champion

    Hi,

    Yes you need both objects but they really don't need to know each other. e.g. QSerialPort doesn't need to know that you connected it to another class that will drive an LCD and that class also don't need to know that it's a QSerialPort that is feeding it data, one is a producer and the other a consumer and can be replaced easily if needed.

    Hope it helps



  • Ok, I see that the main advantage is that you can emit a signal to multiple recipients, which the functor is not able to, since it's a 1:1 relationship.

    Would you then restrict the signals/slots to only GUI-related information passing? As a proper way of developing Qt applications, I mean...


  • Lifetime Qt Champion

    No, certainly not. There are no such limitation.


  • Moderators

    @panch said:

    Would you then restrict the signals/slots to only GUI-related information passing? As a proper way of developing Qt applications, I mean...

    QNetworkAccessManager emits signals to tell the world that a download has completed. QSerialPort emits signals to tell the world that some data has been received. QMediaPlayer emits signals to tell the world that its playback status has changed.

    These show that signals and slots are "proper" for developing all kinds of things, not just GUIs.



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.