[Qt-interest] Objective-C like message-passing
Ross Bencina
rossb-lists at audiomulch.com
Tue Sep 15 08:15:00 CEST 2009
Hi Peter
I use QMetaObject::invokeMethod a little bit for this kind of thing. It's
pretty easy to use once you realise that it doesn't use the same method
signature syntax as connect():
http://doc.trolltech.com/4.5/qmetaobject.html#invokeMethod
I'm not sure what advantages/disadvantages your idea has over this though...
Ross.
----- Original Message -----
From: "Peter Kümmel" <syntheticpp at gmx.net>
To: <qt-interest at trolltech.com>
Sent: Tuesday, September 15, 2009 9:14 AM
Subject: [Qt-interest] Objective-C like message-passing
> One main feature of Objective-C/Smalltalk is their message-passing.
> Using this mechanism all OOP is done on a dynamic typing and late-binding
> basis:
> http://en.wikipedia.org/wiki/Objective-C
> Very different here, is C++ with its compile time polymorphism.
>
> Having Qt it isn't very hard to add Objective-C like message-passing to
> C++/Qt.
>
> I've pushed such a extension to gitorious:
> http://qt.gitorious.org/~syntheticpp/qt/syntheticpp/commits/message-passing
>
>
> What is the best way to do such message-passing using C++?
> First I had the idea to also use [] and to overload operator[] on QObject,
> but
> this could break existing code where already operator[] is overloaded.
> After several other ideas I ended here:
>
> - passing a message to a QObject which doesn't takes arguments and returns
> nothing:
>
> obj->take("slotname")(); or
>
> qmsg("slotname").to(obj)(); or
>
> qmsg(obj, "slotname")();
>
>
> - passing a message to a QObject which takes a argument and returns
> nothing:
>
> obj->take("slotname")(111); or
>
> qmsg("slotname").to(obj)(111); or
>
> qmsg(obj, "slotname")(111);
>
>
> - passing a message to a QObject which takes a argument and returns a
> value:
>
> Type t = obj->take("slotname").reply<Type>(1111); or
>
> Type t = qmsg("slotname").to(obj).reply<Type>(1111); or
>
> Type t = qmsg(obj, "slotname").reply<Type>(1111);
>
>
> I wonder if there are better ways to do these calls, any ideas?
>
> Example code could be seen here:
> http://qt.gitorious.org/~syntheticpp/qt/syntheticpp/blobs/message-passing/examples/messagepassing/simple/main.cpp
>
> And typedefing QMetaMessage to qmsg allows it to use unpatched Qts by
> simply including
> http://qt.gitorious.org/~syntheticpp/qt/syntheticpp/blobs/message-passing/src/corelib/kernel/qmetamessage.h
>
>
>
> It is very interesting to have such non-C++ way of invoking methods on
> objects where at runtime only is known that they are a QObjects (Qt's
> NSObject).
> Using message-passing makes it possible 'think different' about
> architectures.
> it's at the other end of compile-time type-checking.
>
> It would also be interesting to implement more Objective-C like behavior
> with Qt.
>
>
> Cheers,
> Peter
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
More information about the Qt-interest-old
mailing list