[Qt-interest] Basic question on applying QObject principles to Interfaces

Mandeep Sandhu mandeepsandhu.chd at gmail.com
Tue May 4 15:46:56 CEST 2010


Hi All,

I had a basic doubt on using QObject's in some scenario's. I'll use a
sample class to illustrate the usage scenario:

class MyInterface : public QObject
{
    Q_OBJECT
public:
    virtual ~MyInterface() {}
    virtual void someMethod() = 0;
signals:
    void mySignal();
};

class MyInterfaceImpl : public MyInterface
{
    Q_OBJECT
public:
    MyInterfaceImpl() {}
    ~MyInterfaceImpl() {}

    void someMethod() {
        // do something
        emit mySignal();
    }
signals:
    void mySignal();
}

A user is only expected to know about the interface class (i.e
MyInterface) for knowing which all methods are available and what
signal is emitted.

Signals/Slots in Interface's/Abstract class:

* Is it ok to declare signals/slots in the interface class even though
only it's sub-class is actually emitting the signal. Whats the
recommended way of telling the user of an interface about what
signals/slots are available?

Using QObjects object tree hierarchy for managing child objects:

* How do I make use of the QObject tree for managing auto-deletion of
child objects. Normally one would pass a pointer of the parent in the
constructor and then initialize the QObject constructor with the same
parent. But since the interface is actually an abstract class iI can;t
do that.

Is the following modification to MyInterfaceImpl the way to do it?

class MyInterfaceImpl : public MyInterface, QObject
{
    Q_OBJECT
public:
    MyInterfaceImpl(QObject *parent = 0) : QObject(parent) {}
...
...
};

I'm not sure of this approach since now I have 2 QObject's in my class
hierarchy.

Your thoughts.

Thanks,
-mandeep



More information about the Qt-interest-old mailing list