title: How to use enums in Qt signals and slots
body: <p>I have some trouble with using <code>enum</code> types in signals. Basicly I have two classes, a state machine and a thread handling the state machine. When the state is changed I want to send a signal with the new state. I also want to represent the state using an <code>enum</code>. In my full blown code the state machine is implemented in a separate shared library, but the code below gives the exact same error.</p>
  
  <p>When I run the code I get the following behaviour:</p>
  
  <pre><code>kotte@EMO-Ubuntu:sigenum $ ./sigenum 
  Object::connect: No such slot MyThread::onNewState(state)
  Test signal 
  Test signal 
  ...
  </code></pre>
  
  <p>I have four files in my sample code: <code>statemachine.h</code>, <code>statemachine.cpp</code>, <code>main.h</code> and <code>main.cpp</code>. The main function simply starts the thread, the thread then creates an instance of the <code>StateMachine</code> and processes signals from the <code>StateMachine</code>. I am pretty new to Qt, so I was a bit puzzled when I realised that you have to enclose the enum with <code>Q_ENUMS</code> and register it with the type system. So It's fully possible that I've made some rookie mistake</p>
  
  <p>The code below is a bit long, but I wanted it to be as similar to my real code as possible.</p>
  
  <p><code>statemachine.h</code> looks like:</p>
  
  <pre class="lang-c prettyprint-override"><code>// statemachine.h
  #ifndef _STATEMACHINE_H
  #define _STATEMACHINE_H
  
  #include &lt;QtCore&gt;
  
  class StateMachine : public QObject
  {
      Q_OBJECT
      Q_ENUMS(state)
  
  public:
      enum state {S0, S1, S2};
  
      void setState(state newState);
  
  signals:
      void stateChanged(state newState);
      void testSignal(void);
  };
  
  Q_DECLARE_METATYPE(StateMachine::state);
  
  #endif
  </code></pre>
  
  <p>And it is implemented as:</p>
  
  <pre class="lang-c prettyprint-override"><code>// statemachine.cpp
  #include &lt;QtCore&gt;
  
  #include "statemachine.h"
  
  void StateMachine::setState(state newState)
  {
      emit stateChanged(newState);
      emit testSignal();
  }
  </code></pre>
  
  <p>The thread is defined as</p>
  
  <pre class="lang-c prettyprint-override"><code>// main.h
  #ifndef _MAIN_H
  #define _MAIN_H
  
  #include &lt;QtCore&gt;
  
  #include "statemachine.h"
  
  class MyThread : public QThread
  {
      Q_OBJECT
  
  private:
      void run(void);
  
  private slots:
      void onNewState(StateMachine::state);
      void onTestSignal(void);
  
  private:
      StateMachine *myStateMachine;
  };
  
  #endif
  </code></pre>
  
  <p>And it is implemented as follows:</p>
  
  <pre class="lang-c prettyprint-override"><code>// main.cpp
  #include &lt;QtCore&gt;
  #include &lt;QApplication&gt;
  
  #include "statemachine.h"
  #include "main.h"
  
  void MyThread::run()
  {
      myStateMachine = new StateMachine();
  
      qRegisterMetaType&lt;StateMachine::state&gt;("state");
  
      // This does not work
      connect(myStateMachine, SIGNAL(stateChanged(state)),
              this, SLOT(onNewState(state)));
  
      // But this does...
      connect(myStateMachine, SIGNAL(testSignal()),
              this, SLOT(onTestSignal()));
  
      forever {
          // ...
          myStateMachine-&gt;setState(StateMachine::S0);
      }
  }
  
  void MyThread::onTestSignal()
  {
      qDebug() &lt;&lt; "Test signal";
  }
  
  void MyThread::onNewState(StateMachine::state newState)
  {
      qDebug() &lt;&lt; "New state is:" &lt;&lt; newState;
  }
  </code></pre>