PDA

View Full Version : qobject_cast on a base class



bunjee
8th June 2009, 11:05
Hey guys, I have the following class:


class myWidget : public QObject, public myInterface

- Now I'm in myInterface::myFunction()
- I want to cast "this" into myWidget:


qobject_cast<myWidget *> (this);

This doesn't seem to work anyone knows why?

wysota
8th June 2009, 13:40
Do you have Q_OBJECT macro in your class?

bunjee
8th June 2009, 14:42
Let me clarify what I was trying to do:

I have a myAbstractClass supposed to be used like that :


class myObject : public QObject, public myAbstractClass

But,

In my library, I'm not defining the myObject class, just the myAstractClass.

It's "supposed" to be used with a QObject all the time to have the benefit of parency. (which is a bad design at this point).

So I wanted to qobject_cast that base class in QObject assuming it was inheriting QObject.

In that "ugly" case I guess dynamic_cast is my only option.

wysota
8th June 2009, 21:45
Use Q_DECLARE_INTERFACE in myAbstractClass. Then you'll be able to use object_cast. And remember about Q_OBJECT in the subclass.