Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Signal without connection even in event loop?
Qt 6.11 is out! See what's new in the release blog

Signal without connection even in event loop?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 3.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? This user is from outside of this forum
    ? This user is from outside of this forum
    Guest
    wrote on last edited by
    #1

    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? :-)

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      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.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3

        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]

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        0
        • ? This user is from outside of this forum
          ? This user is from outside of this forum
          Guest
          wrote on last edited by
          #4

          Thanks to both of you!

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved