[Qt-interest] Basic question on applying QObject principles to Interfaces
Oliver.Knoll at comit.ch
Oliver.Knoll at comit.ch
Tue May 4 17:35:33 CEST 2010
> ...
> 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) {}
> ... ...
> };
That's exactly how I did it as well in practise: my "use case" was a "plugin interface" and I wanted the application to connect to signals, emitted only by the concrete plugin implementation. So in order to be able to connect to a signal, it had to be defined in the interface class (which on its turn then needs to be mocced etc.) which is the only known class to the application (the "application code" must off course not reference any plugin-related code).
> 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.
Totally agree, but in practise I just said that except for the auto-generated code ("emitting the signals"), as long as all other methods are pure virtual, still call it an "interface".
The only practical little problem I hit with this approach is that the plugin code cannot directly inherit from other Qt classes such as QThread, QWidget etc. But most often it is better that a plugin "has a" widget (a custom configuration dialog, for instance) instead of "is a" widget (or "has a" worker thread instead of "is a" etc.), so you avoid multiple inheritance:
class RendererInterface : public QObject {
Q_OBJECT
public:
...
QWidget *getRenderer() = 0;
public signals:
void finished();
};
class SuperRenderer : public RendererInterface {
Q_OBJECT
public:
...
QWidget *getRenderer() {
return m_mySuperRendererWidget;
}
...
private:
MySuperRendererWidget *m_mySuperRendererWidget;
};
Cheers,
Oliver
--
Oliver Knoll
Dipl. Informatik-Ing. ETH
COMIT AG - ++41 79 520 95 22
More information about the Qt-interest-old
mailing list