How define an interface for a Qt object
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
Re: How define an interface for a Qt object
Take a look at http://doc.trolltech.com/4.3/uitools...heritance.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
Re: How define an interface for a Qt object
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();
...
Re: How define an interface for a Qt object
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()
{}
Re: How define an interface for a Qt object
Make ICallHandler have a protected ctor which takes a QObject* as a parameter. Then in the ctor of CallHandler, pass the parent. For example:
Code:
class ICallHandler
: public QObject{
public:
virtual ~ICallHandler(){};
public slots:
virtual void onIncomingCall() = 0;
protected:
};
Then in the CallHandler ctor:
Code:
CallHandler
::CallHandler(QObject *parent
) :
ICallHandler(parent)
{
}
Re: How define an interface for a Qt object
Is it possible to not have dependencies to Qt in the interface?
For me the ICallHandler interface shall be independent of the UI.
Re: How define an interface for a Qt object
Quote:
Originally Posted by
vince
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.
Re: How define an interface for a Qt object
Also QObject is a class of QtCore so it does not have any dependency on UI even when being used.
Cheers,
_