Results 1 to 8 of 8

Thread: How define an interface for a Qt object

  1. #1
    Join Date
    Dec 2009
    Posts
    62
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default 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

  2. #2
    Join Date
    Feb 2007
    Posts
    73
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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

  3. #3
    Join Date
    Dec 2009
    Posts
    62
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default 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();
    ...

  4. #4
    Join Date
    Dec 2009
    Posts
    62
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default 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:nIncomingCall()
    {}

  5. #5
    Join Date
    Feb 2007
    Posts
    73
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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:
    Qt Code:
    1. class ICallHandler : public QObject
    2. {
    3. public:
    4. virtual ~ICallHandler(){};
    5.  
    6. public slots:
    7. virtual void onIncomingCall() = 0;
    8.  
    9. protected:
    10. ICallHandler(QObject *parent = NULL) : QObject(parent) {}
    11. };
    To copy to clipboard, switch view to plain text mode 

    Then in the CallHandler ctor:
    Qt Code:
    1. CallHandler::CallHandler(QObject *parent)
    2. :
    3. ICallHandler(parent)
    4. {
    5. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Aug 2013
    Posts
    1

    Default 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.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How define an interface for a Qt object

    Quote Originally Posted by vince View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

Similar Threads

  1. (ActiveQt) Invoking interface from COM object
    By mridulgandhi in forum Qt Programming
    Replies: 4
    Last Post: 11th October 2009, 20:29
  2. writing object to the file(Object Persistance)
    By jjbabu in forum Qt Programming
    Replies: 2
    Last Post: 11th June 2009, 14:28
  3. tr with #define..it can work?
    By mattia in forum Newbie
    Replies: 9
    Last Post: 4th February 2008, 11:15
  4. #define MYCLASS_H ?
    By bnilsson in forum General Programming
    Replies: 1
    Last Post: 3rd February 2008, 10:50
  5. where should i put #define in QtDesigner?
    By nass in forum Qt Tools
    Replies: 1
    Last Post: 16th October 2006, 15:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.