Signal without connection even in event loop?



  • Hey there,
    I've got a short question about the Signal-Slot-Concept of Qt. If I have a class which emits a signal and no one is connected to this signal, will there be any data in the event loop of this "emit"-statement? Or just ignores the MOC this statement?

    A short example:

    main.cpp
    @#include <QCoreApplication>
    #include "MyClass.h"

    int main(int argc, char argv[])
    {
    QCoreApplication app(argc, argv);
    MyClass
    pObject = new MyClass();
    pObject->start();

    return app.exec&#40;&#41;;
    

    }
    @

    MyClass.h
    @
    #include <QObject>

    class MyClass : public QObject
    {
    Q_OBJECT
    public:
    explicit MyClass(QObject *parent = 0);

    signals:
    void emitSignal();

    public slots:
    void start();
    };
    @

    MyClass.cpp
    @
    #include "MyClass.h"

    MyClass::MyClass(QObject *parent) :
    QObject(parent)
    {
    }

    void MyClass::start() {
    emit emitSignal();
    }
    @

    Can anybody help? :-)


  • Moderators

    basically the emit enters the corresponding moc-class, which can't find any established connection to a signal/slot and just returns.

    Btw. the event loop is not involved at all in most cases. Only if the connection is made with an queued type.


  • Moderators

    raven-worx is right.

    Every time you emit a signal, it calls QMetaObject::activate(), even if no slot is connected. (You never need to call this function directly though)

    Read "How Qt Signals and Slots Work":http://woboq.com/blog/how-qt-signals-slots-work.html. The section called "Signal Emission" describes QMetaObject::activate():
    [quote][If] there is nothing connected to this signal, ... we can return quickly, which means emitting a signal connected to no slot is extremely fast.[/quote]



  • Thanks to both of you!


Log in to reply
 

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