Advertisement
Guest User

Untitled

a guest
May 13th, 2013
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1. /* main.cpp */
  2.  
  3. #include <QCoreApplication>
  4. #include "runner.h"
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8.     QCoreApplication a(argc, argv);
  9.     Runner *runner = new Runner;
  10.     runner->start();
  11.     return a.exec();
  12. }
  13.  
  14. /* ifoo.h */
  15.  
  16. #ifndef IFOO_H
  17. #define IFOO_H
  18.  
  19. #include <QObject>
  20.  
  21. class IFoo
  22. {
  23. public:
  24.     virtual void fooMethod() = 0;
  25.     virtual void fooSlot() = 0;
  26.     virtual void fooSignal() = 0;
  27. };
  28. Q_DECLARE_INTERFACE(IFoo, "ifoo/1.0")
  29. #endif // IFOO_H
  30.  
  31.  
  32. /* runner.h */
  33. #ifndef RUNNER_H
  34. #define RUNNER_H
  35.  
  36. #include <QObject>
  37. #include <QDebug>
  38. #include "ifoo.h"
  39.  
  40. class Runner : public QObject, public IFoo
  41. {
  42.     Q_OBJECT
  43.     Q_INTERFACES(IFoo)
  44. public:
  45.     Runner(QObject * parent = 0) : QObject(parent)
  46.     {
  47.     }
  48.  
  49.     void start()
  50.     {
  51.         qDebug() << "start";
  52.         IFoo *runner = new Runner;
  53.         //ideally I would love this to work:
  54.         ////connect(runner, SIGNAL(fooSignal()), this, SLOT(fooSlot()))
  55.         //that is, the interface would declare signals and slots
  56.         //technically it's possible, see this article: http://doc.qt.digia.com/qq/qq15-academic.html#multipleinheritance (by using composition instead)
  57.         //but it does not provide all the code that I could run and test and I can't get it right.
  58.  
  59.         //qobject_cast<Runner*>(runner) fails here, static_cast works
  60.         connect(static_cast<Runner*>(runner), SIGNAL(fooSignal()), this, SLOT(fooSlot()));
  61.         runner->fooMethod();
  62.     }
  63.     void fooMethod()
  64.     {
  65.         qDebug() << "fooMethod called";
  66.         qDebug() << "emitting fooSignal";
  67.         emit fooSignal();
  68.     }
  69.  
  70. public slots:
  71.     void fooSlot()
  72.     {
  73.         qDebug() << "fooSlot called";
  74.     }
  75.  
  76. signals:
  77.     void fooSignal();
  78. };
  79.  
  80. #endif // RUNNER_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement