24

For signal and slot of below type

signals:
    void textChanged(const QString &);

public slots:
    void setText(const QString & text)

the type of argument of textChanged and setText seems to work invarable of const and &. Does the constant and reference qualification make any difference compared to just using QString ?

QObject::connect(a,SIGNAL(textChanged(QString)),b,SLOT(setText(QString)));
QObject::connect(a,SIGNAL(textChanged(const QString &)),b,SLOT(setText(const QString &)));

EDIT: I did not notice the output window showing error messages when there is incompatible type being used in SIGNAL or SLOT. I thought the signal slot mechanism is capable of detecting argument type error at compile time.

yesraaj
  • 46,370
  • 69
  • 194
  • 251

2 Answers2

28

Qt checks a normalized signature, meaning

Normalization reduces whitespace to a minimum, moves 'const' to the front where appropriate, removes 'const' from value types and replaces const references with values.

feedc0de
  • 3,646
  • 8
  • 30
  • 55
e8johan
  • 2,899
  • 17
  • 20
  • Is there any article that explains fully abt how Qt implements signal and slot mechanism? other than http://doc.trolltech.com/4.6/signalsandslots.html – yesraaj Dec 20 '09 at 14:34
  • Do you mean fully as in greater detail, or fully as a tutorial walking you through it, but at another pace? – e8johan Dec 21 '09 at 06:52
  • with greater details, any link is appreciated :) – yesraaj Dec 21 '09 at 10:06
  • 2
    I hate when people point me to the source, but start at the QMetaObject::normalizedSignature function and work your way from there. It contains quite a bit of string parsing states, but once you see through that it is quite simple. You can read the source here: http://qt.gitorious.org/qt/qt/blobs/master/src/corelib/kernel/qmetaobject.cpp . The connections are being made using the QMetaObjects, so that is where all the magic is. – e8johan Dec 21 '09 at 10:42
  • So this means you can't have two signals with the same name, one that takes a value and one that takes a const reference? – Claudiu Aug 17 '15 at 22:14
4

Disclaimer: My qt is rather rusty, but the signal/slot mechanism is still just C++ function calls. If the signal/slot mechanism actually copies objects into internal storage, my apologies (you'll need to check the Qt pages, there's a big one on signals/slots afaik) - as the bits below will only be relevant in a C++ context, not in a C++ + Qt context.

If you leave out the reference, the string will be copied (and having the const would not matter, any changes made to it will remain in the function alone).
If you leave in the reference but take out the const, you allow the method to modify the string that you give it. They both work, but do different things to the object you pass (amount of copying/possibility of retaining changes).

I suggest you read the following resources:

(on const correctness) https://isocpp.org/wiki/faq/const-correctness

(on references) https://isocpp.org/wiki/faq/references

to understand exactly what passing a parameter is and how void foo(const A&)/ void foo(const A)/ void foo(A&)/ void foo(A) are all different.

seaotternerd
  • 6,298
  • 2
  • 47
  • 58
laura
  • 7,280
  • 4
  • 35
  • 43
  • 5
    For direct connections, it is recommended that the values are passed by const reference to avoid needless copies. For queued connections Qt will make a copy no matter how you pass the arguments. – rpg Dec 21 '09 at 09:25