[Qt-interest] signals and slots....revisited...

Arnold Krille arnold at arnoldarts.de
Mon Mar 14 09:47:36 CET 2011


Hi,

On Sunday 13 March 2011 19:00:34 sarvesh saran wrote:
> I have been a Qt programmer for quite some time now and i understand most
> of the general features of Qt. I am still confused  about how signals and
> slots work...

It amazes me how someone can use Qt for longer time and not understand 
signals/slots. That is one of the first things to learn…

> this is what i know (correct me if i am wrong)
> 
>    1. slots are like ordinary member functions.however they can be called
>    via signals irrespective of their access specifier.

Yes, slots are ordinary function, no, 'calling' them from signals can not 
invalidate access specifiers.

>    2. emit is a macro that expands to nothing.

Correct, it helps the eye of the developer.

>    3. when you write emit mysignal() you are just calling a function called
>    mysignal which has been implemented in the generated moc file.

Correct. The secret lies within this function: It checks the connection list 
which slots are connected and will be executed. Furthermore it has a 
connection-type (Direct, Queued or auto) which helps deciding which slot to 
call in what way:
 - Direct connections and auto-connections with receivers in the same thread 
are executed directly.
 - Queued connections and auto-connections with receivers in another thread 
are converted to events ("call that slot with these arguments") and will then 
execute when the receivers event-loop allows.

>    4. for every object (instantiated from a class containing Q_OBJECT macro
>    and inheriting QObject) an addition meta object is instantiated ?
>    5. QT stores the names slots and signals of an object in its  meta
>    object? in what format.....<name,function pointer> ?

What these MetaObjects do and contain is mostly an implementation-detail to 
make things work. It doesn't really affect how you use signals and slots.

>    6. connections between objects are made at runtime...how?? not sure how
>    and where a slot is searched for....

You should have heard about how connections between signals and slots (and 
connections between signals and signals!) are made. In case you missed it, 
QObject has this nice connect(…)methods…

>    7. but the arguments in the connect call are matched at compile time?

No, at runtime.

>    8. while connecting, only the arguments of the slot is checked...surplus
>    arguments are discarded.

yep, that allows for buttons to emit "clicked(bool state)" and be usable for 
slots like "btn_clicked(bool state)" and "btn_clicked()" at the same time.

>    9. SIGNAL and SLOT are macros......not sure what their purpose is....

When you look at the documentation (oh, I said it) of QObject, you will notice 
that the arguments to connect are "QObject*, const char*, QObject*, const 
char*". The macros SIGNAL and SLOT "just" convert whatever you give them into 
a string that QObject::connect can understand.

> i tried to look into the generated moc file but things are still murky..any
> help will be greatly appreciated.

Don't look at a moc-file unless you know about the internal ways of the Qt 
Metaobject system and want to debug a problem.

Generally speaking: Instead of looking at the code, you would have learned 
more with looking at the documentation.

Have fun,

Arnold
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110314/f1b3f511/attachment.bin 


More information about the Qt-interest-old mailing list