title: QStateMachine with QSignalTransition loses signal
body: <p>i have a strange problem with QStateMachine, which i cannot solve for a week.</p>
  
  <p><strong>Brief explanation:</strong></p>
  
  <p>I use QStateMachine in my application for controlling of sending commands to a biomedical device, connected through COM port. The whole state machine is complicated with hundreds of states and frequent substating. </p>
  
  <p>Machine is being generated from device's communication protocol, based on user's preference. For example, if user wants some "reaction" from device, this reaction is composed of several subreactions that consist of elementary steps (instructions for the device). Application builds whole reaction from these elementary steps and starts it on state machine.</p>
  
  <p>Therefore, I've created class <em>CompositeReactionControl</em> (inherited from QState) which allows to build a composite from particular substates.</p>
  
  <p>QSignalTransition instances are used to connect states together.</p>
  
  <p><strong>Problem description:</strong></p>
  
  <p>Sometimes the machine does not transit from one state to another, when finish() signal of previous state is emitted.</p>
  
  <p><strong>NOTES:</strong></p>
  
  <ol>
  <li>I am 100% sure that state is connected.</li>
  <li>I am 100% sure that signal is fired.</li>
  <li>Event through machine and its states run in separate thread from the rest of application, this particular signal is fired and caught in SAME thread</li>
  <li>It happens randomly in all reactions (there is no particular reaction or substate connected with this problem)</li>
  <li>Substate which caused problem in one run (reaction), runs without any problem in other runs of same reaction</li>
  <li>It happens when StateMachine runs for long time, it never appears before 4-th reaction (no matter which reactions are chosen by user).</li>
  </ol>
  
  <p><strong>CODE</strong></p>
  
  <p>Problematic parts are marked with comment "HERE IS PROBLEM" in the following code...</p>
  
  <p><strong>Adding of new substate to CompositeReactionControl:</strong></p>
  
  <pre><code>/**
   * @brief Add new reaction control as a child of this composite
   * @param reaction child reaction (inherited from QState)
   * @return added child reaction
   */
  AbstractReactionControl* CompositeReactionControl::addChildReaction(
      AbstractReactionControl* reaction) {
  
    // Check whether reaction is valid
    if (reaction == NULL) {
      QString msg = tr("Cannot add NULL subreaction to reaction '%1'.");
      Logger::getInstance()-&gt;addError(msg.arg(getName()), this);
      return NULL;
    }
  
    // Adopt reaction
    reaction-&gt;setParent(this);
  
    // Store previous reaction control and add current to list
    AbstractReactionControl* prev = controls.size() &gt; 0 ? controls.last() : NULL;
    controls.append(reaction);
  
    // Connects current state
    connect(reaction, SIGNAL(reactionEntered(core::AbstractCommunicationState*)),
            SLOT(onSubReactionEntered(core::AbstractCommunicationState*)));
    connect(reaction, SIGNAL(reactionFinished(core::AbstractCommunicationState*)),
            SLOT(onSubReactionFinished(core::AbstractCommunicationState*)));
  
    // If previous state does not exist (this is the first one) then set current
    // state 'c' as initial state and add transition from 'c' to final state
    // Otherwise add transition from previous state to 'c', remove transition from
    // previous state to final state and add transition from 'c' to final state
    if (prev == NULL) {
      this-&gt;setInitialState(reaction);
      this-&gt;firstState = reaction;
    } else {
  
      // Remove end transitions from previous state
      prev-&gt;removeTransition(endTransition1);
      prev-&gt;removeTransition(endTransition2);
      delete endTransition1;
      delete endTransition2;
  
      // Replaced with PassTransition
      //prev-&gt;addTransition(prev, SIGNAL(finished()), reaction);
  
      // HERE IS PROBLEM: I am 100% sure that finished() signal is emitted,
      // but sometimes not caught by transition
      PassTransition* t = new PassTransition(this, prev, SIGNAL(finished()));
      t-&gt;setTargetState(reaction);
      prev-&gt;addTransition(t);
    }
  
    // Assign new end transitions to current state
    endTransition1 = new RepeatTransition(this, reaction, SIGNAL(finished()));
    endTransition2 = new FinishTransition(this, reaction, SIGNAL(finished()));  
    endTransition1-&gt;setTargetState(firstState);
    endTransition2-&gt;setTargetState(allFinished);
    reaction-&gt;addTransition(endTransition1);
    reaction-&gt;addTransition(endTransition2);
  
    // Finish if halt
    reaction-&gt;addTransition(this, SIGNAL(halt()), allFinished);
  
    // Exit reaction
    return reaction;
  }
  //---------------------------------------------------------------------------
  </code></pre>
  
  <p><strong>PassTransition:</strong></p>
  
  <pre><code>/**
   * @class PassTransition
   * @brief Passes control from one substate to another
   * @author Michal Rost
   * @date 6.11.2013
   */
  class PassTransition : public QSignalTransition {
  public:
    PassTransition(CompositeReactionControl* owner, QObject* sender,
                   const char *signal) : QSignalTransition(sender, signal) {
      this-&gt;owner = owner;
    }
  protected:
    CompositeReactionControl* owner;
    bool eventTest(QEvent *event) {
  
      // HERE IS PROBLEM: I know that signal is called, but event is not received
  
      QString msg = tr("Event Test %1, source: %2   target: %3");
      AbstractReactionControl* s = dynamic_cast&lt;AbstractReactionControl*&gt;(this-&gt;sourceState());
      AbstractReactionControl* t = dynamic_cast&lt;AbstractReactionControl*&gt;(this-&gt;targetState());
      common::Logger::getInstance()-&gt;addDebug(msg.arg(event-&gt;type()).arg(s-&gt;getName()).arg(t-&gt;getName()), this);
      return QSignalTransition::eventTest(event);
    }
    void onTransition(QEvent *event) {
      QSignalTransition::onTransition(event);
      QString msg = tr("Transition called");
      common::Logger::getInstance()-&gt;addDebug(msg, this);
    }
  };
  </code></pre>
  
  <p><strong>Log from good case</strong></p>
  
  <p>In this case everything works well. Important is that PassTransition::EventTest receives event of type 192, which is state machine event created from finished() signal of previous state.  </p>
  
  <pre><code>[08:33:01:976][core::CompositeReactionControl] State 'send_fluorescence_on' finished, disconnecting...
  [08:33:01:976][core::CompositeReactionControl] State 'ProfileData' removed control from 'send_fluorescence_on' substate.
  [08:33:01:977][core::ReactionEmitter] Sending state entered. Timer Started.
  [08:33:01:977][core::PassTransition] Event Test 0, source: send_fluorescence_on   target: send_fluorescence_off
  [08:33:01:977][core::PassTransition] Event Test 0, source: measure_fluorescence   target: measure_fluorescence
  [08:33:01:977][core::PassTransition] Event Test 192, source: send_fluorescence_on   target: send_fluorescence_off
  [08:33:01:977][core::PassTransition] Transition called
  [08:33:01:977][core::CompositeReactionControl] State 'ProfileData' gave control to 'send_fluorescence_off' substate.
  </code></pre>
  
  <p><strong>Log from bad case</strong></p>
  
  <p>As can be seen, if error RANDOMLY occurs, mentioned event is not received by method PassTransition::EventTest. After 5 second timeout (I tryed even longer intervals), I stop the statemachine and print error. </p>
  
  <pre><code>[08:33:01:991][core::CompositeReactionControl] State 'send_fluorescence_off' finished, disconnecting...
  [08:33:01:991][core::CompositeReactionControl] State 'ProfileData' removed control from 'send_fluorescence_off' substate.
  [08:33:01:991][core::ReactionEmitter] Sending state entered. Timer Started.
  [08:33:01:991][core::PassTransition] Event Test 0, source: send_fluorescence_off   target: led_off
  [08:33:01:991][core::PassTransition] Event Test 0, source: measure_fluorescence   target: measure_fluorescence
  [08:33:16:966][core::ReactionEmitter] State machine is frozen. Reaction will be stopped!
  </code></pre>