signals and slots vs. virtual_hook (was [PATCH] KFileDialog overwrite confirmation)

Thiago Macieira thiago at
Tue Jul 15 23:00:06 BST 2008

Leo Savernik wrote:
>Am Dienstag, 15. Juli 2008 schrieb Thiago Macieira:
>> Besides, my point is exactly that qt_metacall *is* the same as
>> virtual_hook. There's no need to involve signals here. Technically all
>> you needed to do was:
>> void MiddleClass::publicFunction(bool b)
>> {
>>     void *args[] = { &b };
>>     int id = metaObject()->indexOfMethod("privateFunction(bool)");
>>     qt_metacall(QMetaObject::InvokeMetaMethod, id, args);
>> }
>Cool, I didn't know that! It's ugly, but it should still be faster than
> a slot invocation.

The above is what the invokeMethod() call does. It hides the gory details 
from you, but that's all.

You should use it because the above actually contains an error. It should 
have been:

	void *args[] = { 0, &b };

args[0] is used to hold the return value. (Put a variable there and 
qt_metacall will store the return value for you in it)

Note, however, that indexOfMethod takes a *normalised* name. The following 
will never work (will return -1):

	indexOfMethod("privateFunction( bool )");
	indexOfMethod("privateFunction(const QString&)");
	indexOfMethod("privateFunction(char const *)");

Using invokeMethod hides the normalisation process from you and also 
provides a modicum of type safety.

>> protected:
>>     Q_INVOKABLE void privateFunction(bool b);
>> };
>Say, I don't have a Q_INVOKABLE in release x. Is then adding Q_INVOKABLE
> in release x+1 BIC?

That's a binary- and source-compatible change. Unlike the virtual table, 
the meta object searches entries by string comparison -- the 
indexOfMethod call above. So you can add and remove as many entries as 
you want.

Also, note that setting the method to Q_SCRIPTABLE or making it a slot 
makes it invokable too. (Slots can be scriptable too)

  Thiago Macieira  -  thiago (AT) - thiago (AT)
    PGP/GPG: 0x6EF45358; fingerprint:
    E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
URL: <>

More information about the kde-core-devel mailing list