Important: Please read the Qt Code of Conduct -

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:

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

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

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


    #include <QObject>

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

    void emitSignal();

    public slots:
    void start();

    #include "MyClass.h"

    MyClass::MyClass(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": 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