Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* main.cpp */
- #include <QCoreApplication>
- #include "runner.h"
- int main(int argc, char *argv[])
- {
- QCoreApplication a(argc, argv);
- Runner *runner = new Runner;
- runner->start();
- return a.exec();
- }
- /* ifoo.h */
- #ifndef IFOO_H
- #define IFOO_H
- #include <QObject>
- class IFoo
- {
- public:
- virtual void fooMethod() = 0;
- virtual void fooSlot() = 0;
- virtual void fooSignal() = 0;
- };
- Q_DECLARE_INTERFACE(IFoo, "ifoo/1.0")
- #endif // IFOO_H
- /* runner.h */
- #ifndef RUNNER_H
- #define RUNNER_H
- #include <QObject>
- #include <QDebug>
- #include "ifoo.h"
- class Runner : public QObject, public IFoo
- {
- Q_OBJECT
- Q_INTERFACES(IFoo)
- public:
- Runner(QObject * parent = 0) : QObject(parent)
- {
- }
- void start()
- {
- qDebug() << "start";
- IFoo *runner = new Runner;
- //ideally I would love this to work:
- ////connect(runner, SIGNAL(fooSignal()), this, SLOT(fooSlot()))
- //that is, the interface would declare signals and slots
- //technically it's possible, see this article: http://doc.qt.digia.com/qq/qq15-academic.html#multipleinheritance (by using composition instead)
- //but it does not provide all the code that I could run and test and I can't get it right.
- //qobject_cast<Runner*>(runner) fails here, static_cast works
- connect(static_cast<Runner*>(runner), SIGNAL(fooSignal()), this, SLOT(fooSlot()));
- runner->fooMethod();
- }
- void fooMethod()
- {
- qDebug() << "fooMethod called";
- qDebug() << "emitting fooSignal";
- emit fooSignal();
- }
- public slots:
- void fooSlot()
- {
- qDebug() << "fooSlot called";
- }
- signals:
- void fooSignal();
- };
- #endif // RUNNER_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement