[Qt-interest] Basic question on applying QObject principles to Interfaces
Thiago Macieira
thiago at kde.org
Tue May 4 16:27:36 CEST 2010
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:
[snip]
> 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
class.
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
{
Q_OBJECT
public:
MyInterface(QObject *parent = 0) : QObject(parent) {}
...
};
class MyInterfaceImpl : public MyInterface
{
Q_OBJECT
public:
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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100504/ef8e3019/attachment.bin
More information about the Qt-interest-old
mailing list