views:

786

answers:

2

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.

+3  A: 

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) http://parashift.com/c++-faq-lite/const-correctness.html

(on references) http://parashift.com/c++-faq-lite/references.html

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.

laura
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
+6  A: 

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.

e8johan
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
Do you mean fully as in greater detail, or fully as a tutorial walking you through it, but at another pace?
e8johan
with greater details, any link is appreciated :)
yesraaj
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
let me try to read through it......
yesraaj