to your example (cast2):
i) please output pointers with %p and without casting them to unsigned int.
This does not compile on my 64bit machine. And just using %p is so much shorter and clearer.
ii) try using qDebug() << "current" << stack.currentWidget();
instead of qDebug("...");
this makes it much simpler to output Qt objects (widgets, lists, QSize etc)
iii) apart from that your exmaple compiles on my suse10.2 linux (amd64) box and both casts fail.
iv) the (potential) failure of dynamic_cast across plugins is one of the reasons the trolls made their own cast (see the docs to QObject, and there qobject_cast)
v) a test with inherits succeeds for me
i.e. stack.currentWidget()->inherits("CW")
(you'd have to cast to CW* with static_cast then)
vi) so, why does qobject_cast fail anyway:
Well, as inherits (slow) succeeds, we know that the type info on A and CW etc actually is present.
The trolls could have made qobject_cast to work, but for efficiency I guess, qobject_cast traverses the qmetaobject (tree) until it finds a the same (pointer identity) meta object. Then the casts was successful. Otherwise it fails. So, obviously the pointers do not match for your example. Why?
You put, however, cw.h into the HEADERS+= of *both* the plugin and your main. This results in 2 mocs generated for cw.h - each of which contains the static meta object for CW. The problem is - just with dynamic_cast I should think - that your plugin creates the CW object with a reference to its own metaobject for CW and your main tests against the other meta object. (Note that these meta objects a equal but not the same - they are two identical copies with a different identity (i.e. address).)
Removing cw.h from the HEADERS causes a link failure. Both parts need to link against this metaobject - but it should be the very same, not two copies of it (see solution ii).
Solutions:
i) use inherits (rather not, I'd prefer the next one)
ii) do NOT put cw.h (resp. the moc code) inside the plugin but rather into a separated library (say libMYPLUGINBASE.so, that both main and the plugin will link against). This way there will be just one instance of CW's metaobject and qobject_cast should work as intended.
HTH
Christoph
Bookmarks