SIgnal/slots or function pointers??



  • What are the pros and cons of the two system of linking classes or methods??


  • Moderators

    @TagLog said in SIgnal/slots or function pointers??:

    of linking classes or methods

    what exactly do you mean by this?



  • What pros/cons of using singal-slot system instead of typical event handler system?
    It's just a different point of views to the same conception. Moreover, signal-slot system, in common case, is implemented having event handlers system under the hood, i.e. "signal-slot" is simply a kind of syntactic sugar and "events", in their turn, is a cascade of callbacks based on function pointers.
    Qt is built around MOC, it brings a powerful introspectional features in run-time and Qt's signal/slot fully utilizes it but in order to use it your class must be a descendant of QObject, it's a main and single constraint.
    It my case, for example, I use functional pointers in pure C++ classes because these ones slighly faster than QObject derivatives with metaobject overhead. But in a code with tight interactions with Qt componens I use singnal-slots, if it allows me to get what I want to implement. By the way, Qt's singnal-slot system isn't that strict, so you can bind signal with lambda, for example.
    Feel the Force, Luke.


  • Moderators

    @Tikani
    Qt's signal/slots concept isn't necessarily bound to events! Only if the invokation is triggered queued (e.g. between threads)

    One advantage is that you do not need to know the exact type of the instance. It's enough when you know it's a QObject instance. This lets the MOC abstract the whole logic.
    Whereas with function pointers you need to know the exact type to be able to call them.

    If you can live with function pointers, then go with them.

    Calling methods via the meta-object also enables the following:

    bool successfulCalled = QMetaObject::invokeMethod( onObj, "method", Qt::QueuedConnection, Q_ARG(int,arg1) );
    if( !successfulCalled )
    {
         // object doesn't have a method with such signature
    }
    


  • SIgnal/slots by default of Qt::QueuedConnection but function pointers is Qt::DirectConnection.
    Moreover it is recommended to use Signals/Slots in multi thread affinities


  • Moderators

    @Arvindhan-Ponnusamy said in SIgnal/slots or function pointers??:

    SIgnal/slots by default of Qt::QueuedConnection

    who says that?!
    By default Qt::AutoConnection is used. Which results in Qt::DirectConnection if sender and receiver are in the same thread, Qt::QueuedConnection otherwise.



  • @raven-worx By default Qt::AutoConnection is used. Which results in Qt::DirectConnection if sender and receiver are in the same thread, Qt::QueuedConnection otherwise.
    Yes.It is correct.
    But what i was trying to say is about multi threaded affinity.


Log in to reply
 

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