title: Qt4 Widget: implementing a wait function without blocking the widget
body: <p>I'm writing a simple game in C++ using the (awesome) Qt4 libraries. For a while now, I'm having trouble with the following:</p>
  
  <p>The widget provides (among others) 2 methods, that change the way the widget responds to user input like mouse-movement an clicks etc. Let's call these functions <code>fun1()</code> and <code>fun2()</code> to avoid context. All these functions do, is toggle some internal variables in the widget which are checked by all the event-functions (e.g. <code>mouseMoveEvent()</code>). When <code>fun1()</code> is called, the user is expected to perform a move by interacting with the widget and clicking somewhere. The widget checks whether his move was valid and if so, applies his move to the current gamestate. </p>
  
  <p>Now, the main class containing this widget at some point calls <code>fun1()</code> and <code>fun2()</code> subsequently. I have to implement something (elegant) to ensure that the main loop sits around at the end of <code>fun1()</code> until the move has been completed.</p>
  
  <pre><code>void Widget::fun1()
  {
      // Set some members in order to tell the Widget how to respond to events
      // ...
      wait(); // wait for the move to be completed before returning from fun1()
  }
  </code></pre>
  
  <p>How should <code>wait()</code> be implemented whilst maintaining the responsiveness of the GUI, i.e. whilst responding to user interaction?</p>
  
  <p>In the past, I resolved this issue by letting the widget's parent wait for the widget to emit a signal that notifies it has finished. Only when this signal was recieved did I call <code>fun2()</code>. Though this might work fine, it bothers me that it makes the act of waiting the responsibility of the class using the widget instead of the widget itself.</p>
  
  <p>Any suggestions would be very helpful!</p>