[Resolved] Interfaces and Signal/Slot functionality
-
Hello,
I'm trying to create an interface to separate program core from user interface.My idea is to create an interface for user frontend, let's say
@class Interface : public QObject
{
Q_OBJECT
signals:
void userWantsThis();
void userWantsThat();
public slots:
void coreSaysThis();
void coreSaysThat();
}@Now, programmer could create the front-end:
@class Window : public QMainWindow, public Interface {
// ...
}@But AFAIK it won't compile, because Qt, I believe moc to be more specific, doesn't let programmer to inherit QObject more than once (since both QMainWindow and Interface are QObject derivatives).
I have an idea to create such separation via creating signal slot adapter:
@class Adapter : public QObject {
Q_OBJECT
public:
void emitCoreSaysThis() { emit coreSaysThis(); }
void emitCoreSaysThat() { emit coreSaysThat(); }
signals:
void userWantsThis();
void userWantsThat();
void coreSaysThis();
void coreSaysThat();
}@and connecting with frontend to it by signal/slot mechanism.
But I think it's not the elegant solution.
Is there any good practice in creating such ( core | frontend ) separation?
-
Simple solution would be to make your interface class inherit from QWidget for example. This of course only works if QWidget (or QMainWindow) is not to specific for your intended uses.
Another option would be to use your interface class (which is basically a wrapper for QObject) as a member var of the class that will use it.
I think that the QObject class always has to be mentioned first in the deriving hierachy. Its just a guess but maybe this:
@
class Window : public Interface, public QMainWindow {
// ...
}
@
is already a solution to your problem. -
Use eg. boost::signal in the interface.
-
Hi,
By design, shouldn't your Window class use the interface rather than be an interface ?
And for the inheritance problem, no, it's not order dependent (although if you inherit from a QObject, it must be first in the list), you can't inherit from two QObjects (QWidget is a QObject)
-
Thanks, that solves the problem.
-
[quote author="SGaist" date="1361962765"]
And for the inheritance problem, no, it's not order dependent (although if you inherit from a QObject, it must be first in the list), you can't inherit from two QObjects (QWidget is a QObject)[/quote]Thanks for clarifying SGaist. That reminded me of the multiple inheritance problem with a pure virtual class. See "this thread about a similar problem":http://qt-project.org/forums/viewthread/20044. Maybe this could be another useful approach for you videl.