PDA

View Full Version : How define an interface for a Qt object



hubbobubbo
13th January 2010, 17:47
Hi

I am building a dll and I want the user of the dll to pass certain objects to this dll. My intention is to provide an interface to the user but I am not sure if this is the right approach or if it is applicable to Qt.

My idea was something like this:
class ICallHandler
{
public:
virtual ~ICallHandler(){};

//public slots:
virtual void onIncomingCall() = 0;
};

However if I try to put the Qt includes in this .h file I get alot of errors because of multiple inheritance. The other class that would implement the interface would look like this:

#include ".\Interfaces\ICallHandler.h"

#include <QObject>

class CallHandler : public QObject, public ICallHandler
{
Q_OBJECT

public:
CallHandler(QObject *parent);
virtual ~CallHandler();
virtual void onIncomingCall();
};

I guess I could live without defining
public slots:

in the interface .h file but I am not sure if I am doing this the right way. Does any one have suggestions on the best way to achive this. The goal is that the user of the dll should provide a pointer to a class following this interface when calling the dll since I want to connect some signals to slots inside the dll.

Thanks

smacchia
13th January 2010, 18:20
Take a look at http://doc.trolltech.com/4.3/uitools-multipleinheritance.html

It should be possible.

However if 'onIncomingCall' is indeed a slot, then you have to make ICallHandler derive from QObject. If ICallHandler will only be used with Qt apps, then that should be ok. If not, you could use the boost signal/slot library or build your own framework for this.

HTH

hubbobubbo
13th January 2010, 19:13
Thanks for the reply

However if I try to make ICallHandler inherit from QObject I get multiple inheritance from QObject errors:

Warning: Class CallHandler inherits from two QObject subclasses QObject and ICallHandler. This is not supported!

#include ".\Interfaces\ICallHandler.h"

#include <QObject>

class CallHandler : public QObject, private ICallHandler
{
Q_OBJECT

public:
CallHandler(QObject *parent);
virtual ~CallHandler();
//virtual void onIncomingCall();
...

hubbobubbo
13th January 2010, 19:50
Ok so I changed so only the interface derived from QObject and the class inherit from the interface, like below. However I am not sure how to handle the passing of the parent object to the base class. I guess without this I will loose the memory managment, or? See comment in the source file below:

#include <QObject>

class ICallHandler : public QObject
{
public:
virtual ~ICallHandler(){};

public slots:
virtual void onIncomingCall() = 0;
};

and for the class implementing the interface:

#include <QObject>

class CallHandler : public ICallHandler
{
Q_OBJECT

public:
CallHandler(QObject *parent);
~CallHandler();
public slots:
void onIncomingCall();
};

and for the source file:

#include "callhandler.h"

CallHandler::CallHandler(QObject *parent)
: ICallHandler() //How handle the parent object here?
{

}

CallHandler::~CallHandler()
{

}

void CallHandler::onIncomingCall()
{}

smacchia
14th January 2010, 15:07
Make ICallHandler have a protected ctor which takes a QObject* as a parameter. Then in the ctor of CallHandler, pass the parent. For example:


class ICallHandler : public QObject
{
public:
virtual ~ICallHandler(){};

public slots:
virtual void onIncomingCall() = 0;

protected:
ICallHandler(QObject *parent = NULL) : QObject(parent) {}
};


Then in the CallHandler ctor:


CallHandler::CallHandler(QObject *parent)
:
ICallHandler(parent)
{
}

vince
23rd August 2013, 22:22
Is it possible to not have dependencies to Qt in the interface?

For me the ICallHandler interface shall be independent of the UI.

wysota
24th August 2013, 09:29
Is it possible to not have dependencies to Qt in the interface?

For me the ICallHandler interface shall be independent of the UI.

If you don't use any Qt API in the interface then it will be independent of Qt.

anda_skoa
24th August 2013, 12:37
Also QObject is a class of QtCore so it does not have any dependency on UI even when being used.

Cheers,
_