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?
Forum Updated to NodeBB v4.3 + New Features

Signal without connection even in event loop?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.3k 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 22 Jan 2014, 15:21 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
    • R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 22 Jan 2014, 15:38 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
      • J Offline
        J Offline
        JKSH
        Moderators
        wrote on 22 Jan 2014, 16:52 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 22 Jan 2014, 19:02 last edited by
          #4

          Thanks to both of you!

          1 Reply Last reply
          0

          1/4

          22 Jan 2014, 15:21

          • Login

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