[Qt-interest] moc / multiple inheritance issue
Enrico Ros
enrico.qt at email.it
Mon Sep 14 12:11:39 CEST 2009
Hello Jimmy,
I looked at the code too. As Stephen Jackson said, there are 2 "problems".
You have this kind of graph:
.-> QLineEdit -> QWidget -> QObject
SpecialLineEdit
'-> DeclareYourself
If you use: (DeclareYourself*) or reinterpret_cast<DeclareYourself*>
you are "messing with the multiple inheritance chain" (see the path on the
graph above).
You can't even do the C++ static cast for traversing the graph like that, if
you use for example:
static_cast<DeclareYourself*>(specialLineEdit)->declareYourself();
the compiler tells you:
invalid static_cast from type 'QWidget*' to type 'DeclareYourself*'
There are 2 ways to do what you want, either:
static_cast<SpecialLineEdit*>(specialLineEdit)->declareYourself();
or
dynamic_cast<DeclareYourself*>(specialLineEdit)->declareYourself();
- static_cast goes up-only in the graph, so it's good
- dynamic_cast knows The Good Way to handle the conversion (it may even fail
to cast (for example Apples to Elephants), but this is not the case ;-).
Oh, and don't use the virtual inheritance with QObject descendants.
Enrico
On Monday 14 September 2009 02:21:52 Jimmy Cooksey wrote:
> Hello Everyone,
>
> I'm experiencing a strange problem where moc appears to be hijacking a
> method call to my object that is multiply inheriting from some widget
> and another base class.
>
> Here is some sample code:
>
> // This is the base class
> class DeclareYourself
> {
> public:
> DeclareYourself() { };
> virtual ~DeclareYourself() { };
> virtual void declareYourself() = 0;
> };
>
> class SpecialLineEdit : public QLineEdit, public DeclareYourself
> {
>
> Q_OBJECT
>
> public:
> SpecialLineEdit(QWidget* parent) : QLineEdit(parent), DeclareYourself();
> ~SpecialLineEdit() { }
> void declareYourself()
> {
> setText("I'm a line edit!");
> return;
> }
> };
>
> So when I create this object, I'm using something like...
> QWidget* specialLineEdit = new SpecialLineEdit(this);
>
> The interesting thing is that this call...
> ((*DeclareYourself) specialLineEdit)->declareYourself();
> ... never calls SpecialLineEdit::declareYourself()
> It immediately calls this:
> SpecialLineEdit::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
> and of course, I have no idea why.
>
> The code itself compiles just fine, and the application does not crash.
>
> I have created a sample project demonstrating my problem:
> http://www.jimster.org/QtTestCase.zip
>
> Any help would be greatly appreciated!
>
> Thanks,
> Jimmy
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
--
Caselle da 1GB, trasmetti allegati fino a 3GB e in piu' IMAP, POP3 e SMTP autenticato? GRATIS solo con Email.it http://www.email.it/f
Sponsor:
Offerte a Riccione. Prenota ora su vacanzedivertenti.it
Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid=9529&d=14-9
More information about the Qt-interest-old
mailing list