Nominate our 2022 Qt Champions!

Understanding signature normalization for signals and slots

  • Moderators

    Hi all,

    I've just discovered the concept of signature normalization, and would like to improve my understanding of it. Let's say I have 2 classes:

    class Sender : public QObject
        // Emit a copy of the QVector because the object doesn't store a local copy
        void send(QVector<int>, float);
    class Receiver : public QObject
    public slots:
        // Avoid making another copy by using const-&
        void receive(const QVector<int> &, float);

    Previously, I would make the connection by copying the function prototype, and I used lots of whitespace for readability:

    connect::(  sender, SIGNAL( send(QVector<int>, float) ),
            receiver, SLOT( receive(const QVector<int> &, float) )  );

    After some reading, here's my current understanding of the signal/slot connection system. Could someone please check if I've got it right?

    1. A "signature" is a string, comprised of the characters typed into the SIGNAL() and SLOT() macros, or modified by QMetaObject::normalizedSignature()

    2. The meta-object system only stores normalized signatures, so any (probably unnoticeable) "normalization overhead" is only incurred at connection time, not every time the signal is emitted.

    3. In my connection above, I can manually normalize my signatures and avoid a "normalization overhead" by re-writing it as:

        connect::(  sender, SIGNAL(send(QVector<int>,float)),
                receiver, SLOT(receive(QVector<int>,float))  );
    1. Only the function prototypes determine if the parameters are passed-by-value or passed-by-reference; the signatures passed into the SIGNAL() and SLOT() macros have no effect on this

    2. In my example classes above, the QVector is copied once each time the signal is emitted. If Receiver::receive() had used pass-by-value instead, the QVector would be copied twice each time the signal is emitted.

    Thanks in advance!

  • Moderators

    I found this quote in the wiki ( ):

    Always specify the full types of the arguments, i.e. const QString & instead of QString, in the interest of keeping things simple for beginners. We’ll mention that you can write the latter in the documentation, but stick to a consistent, simple subset of the Qt language in examples.

    Is this sound advice?

Log in to reply