[email protected]
[Top] [All Lists]

Re: [Qt-interest] Basic question on applying QObject principles to Inter

Subject: Re: [Qt-interest] Basic question on applying QObject principles to Interfaces
From: Thiago Macieira
Date: Tue, 4 May 2010 16:27:36 +0200
Em Terça-feira 04 Maio 2010, às 15:46:56, Mandeep Sandhu escreveu:
> I had a basic doubt on using QObject's in some scenario's. I'll use a
> sample class to illustrate the usage scenario:

> 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?

It's not a problem. In fact, overriding signals is a bad idea. If a given 
signal is used by many subclasses and has the same meaning, put it in the base 

Qt does that too: QIODevice has readyRead() and bytesWritten(qint64), but 
QIODevice itself doesn't emit those signals. Only subclasses do.

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

Just do it :-)

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

Sure you can. You almost did it yourself:

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

Yes, this code is wrong.

But this would work:

class MyInterface : public QObject
    MyInterface(QObject *parent = 0) : QObject(parent) {}

class MyInterfaceImpl : public MyInterface
    MyInterfaceImpl(QObject *parent = 0) : MyInterfaceImpl(parent) {}

Though I must say, this is not a real "interface" use in the pure sense of the 
word. An interface would not derive from QObject and it would be used in 
multiple-inheritance. That would by necessity mean that the interface contains 
no signals or slots.

Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Senior Product Manager - Nokia, Qt Development Frameworks
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
Qt-interest mailing list
[email protected]
<Prev in Thread] Current Thread [Next in Thread>