views:

136

answers:

1

I have a fairly complex set of C++ classes that are re-written from Java. So each class has a single inherited class, and then it also implements one or more abstract classes (or interfaces).

Is it possible to use qobject_cast() to convert from a class to one of the interfaces? If I derive all interfaces from QObject, I get an error due to ambiguous QObject references. If however, I only have the base class inherited from QObject, I can't use qobject_cast() because that operates with QObjects.

I'd like to be able to throw around classes between plugins and DLLs referred to by their interfaces.

Any ideas?

+2  A: 

After some research and reading the qobject_cast documentation, I found this:

qobject_cast() can also be used in conjunction with interfaces; see the Plug & Paint example for details.

Here is the link to the example: Plug & Paint.

After digging up the interfaces header in the example, I found the Q_DECLARE_INTERFACE macro that should let you do what you want.

First, do not inherit QObject from your interfaces. For every interface you have, use the Q_DECLARE_INTERFACE declaration like this:

class YourInterface
{
public:
    virtual void someAbstractMethod() = 0;
};

Q_DECLARE_INTERFACE(YourInterface, "Timothy.YourInterface/1.0")

Then in your class definition, use the Q_INTERFACES macro, like this:

class YourClass: public QObject, public YourInterface, public OtherInterface
{
    Q_OBJECT
    Q_INTERFACES(YourInterface OtherInterface)

public:
    YourClass();

    //...
};

After all this trouble, the following code works:

YourClass *c = new YourClass();
YourInterface *i = qobject_cast<YourInterface*>(c);
if (i != NULL)
{
    // Yes, c inherits YourInterface
}
Venemo
Doing a .objectCast<IInterface>() creates a compile time error since the routines .objectCast calls on the IInterface are private, and therefore inaccessible by the casting routines.
Timothy Baldridge
@Timothy - After trying to implement it, I found out that my answer was a total fail, so I researched a bit more and edited it. Hope this helps. :)
Venemo
Ahh! Cool. I had tried the Q_DECLARE_INTERFACE, but didn't add the Q_INTERFACES to the derived object. Thanks for the help.
Timothy Baldridge
@Timothy - I'm happy I could help. I learn something new about Qt every day. :)
Venemo
Normally, people use `if (YourInterface *i = qobject_cast<YourInterface*>(c)) { }`. This ensures that the pointer is never used if it is invalid.
DeadMG
@DeadMG - Good point, but I don't think putting `=` into an `if` statement is a good practice, because many people confuse it with `==`.
Venemo
actually I've seen the pattern above used very commonly. Simply because people don't know the difference between = and == is not a good reason to use or not use it. Just like there's no reason to not use ++i vs i++ simply because they could be confused.
Timothy Baldridge
@Timothy - If you like it, use it. :)
Venemo