[Qt-interest] [BROKEN?]"Academic Solutions to Academic Problems"

Mihail Naydenov mlists at ymail.com
Thu Apr 16 23:19:55 CEST 2009


"Academic Solutions to Academic Problems" Qt Quarterly 2005 
http://doc.trolltech.com/qq/qq15-academic.html

In section "Multiple Inheritance" the sample code does not work.

Class ClipboardWrapper uses in its constructor qobject_cast to cast to ClipboardInterface, but ClipboardInterface is not qobject
end the code fails with:
error: 'class ClipboardInterface' has no member named 'qt_check_for_QOBJECT_macro'
error: 'class ClipboardInterface' has no member named 'staticMetaObject'


But the interesting part is, that, when I tried to use different cast, for instance, reinterpret_cast<ClipboardInterface*> or simply (ClipboardInterface*)
It compiled, of course, but for some reason the wrapper (ClipboardWrapper  class) failed to call the wrappedObject functions correctly

For instance:

CustomWidget *cw = new CustomWidget(); 
ClipboardWrapper *cww = new ClipboardWrapper(cw);
cww->copy();

and copy() is defined as

void ClipboardWrapper::copy() const { wrappedObject->copy(); }

but instead of CustomWidget::copy(), the call is to CustomWidget::qt_metacast() (or to qt_metacall(), in other test i made) and basically does nothing usefull and the parent func is never called.

I really have no idea if I misused/misunderstand something, any info will be helpful, because this seams to be the only pattern of interface class with qobject functionality!
I have inline-attached the code from the article that I used for test.
Thank You
MihailNaydneov



class ClipboardInterface 
{
public:
virtual void cut() = 0;
virtual void copy() const = 0;
virtual void paste() = 0;
};

class CustomWidget : public QObject/*QWidget*/, public ClipboardInterface
{
Q_OBJECT
public:
CustomWidget(QObject *parent = 0){;}
void cut(){qDebug("CustomWidget::copy");} // this is never called

void copy() const{;}
void paste(){;}
private:
//ClipboardWrapper *wrapper; // used only for signals

};

class ClipboardWrapper : public QObject, public ClipboardInterface
{
Q_OBJECT
public:
ClipboardWrapper(QObject *parent) : QObject(parent)
{
Q_ASSERT(parent);
wrappedObject =
reinterpret_cast/*qobject_cast*/<ClipboardInterface *>(parent);
Q_ASSERT(wrappedObject);
}
public slots:
void cut() { wrappedObject->cut(); }
void copy() const { wrappedObject->copy(); }
void paste() { wrappedObject->paste(); }
private:
ClipboardInterface *wrappedObject;
};



      




More information about the Qt-interest-old mailing list