title: Best way to get c-stlye callback event to a qt5 signal
body: <p>In my Qt5 project, I am interacting with a piece of hardware through a C-Style library that provides me with event updates through a C-Style callback method. The callback runs on the library thread for which I'd like to return control immediately. My goal is to forward a signal when the hardware library provides me with an event update. I have found a solution shown below but I am not sure if it's the best way. Is there a better method? Would using Qt::postEvent() work better within my registerd callback instead? What would that look like?</p>
  <pre><code>// hw_api.h
  // C-Style library
  class HWLIB {
  public:
      static HWLIB&amp; GetInstance();
      
      void (*_update1CB)(uint8_t, uint8_t);
      void (*_update2CB)(double, double);
      void (*_update3CB)(uint8_t, uint8_t);
  
      void SetUpdate1CB(void (cbFunc)(uint8_t old_value, uint8_t new_value)) {
          _update1CB = cbFunc;
      }
      void SetUpdate2CB(void (cbFunc)(double old_value, double new_value)) {...}
      void SetUpdate3CB(void (cbFunc)(uint8_t old_value, uint8_t new_value)) {...}
  private:
      HWLIB();
  };
  </code></pre>
  <pre><code>// hw_api_interface.h
  class hwi : public QObject {
      Q_Object
  signals:
      // Receiver must use Qt::QueuedConnection
      void status1Changed(bool);
      void status2Changed(double);
      void status3Changed(bool);
  private:
      HWLILB m_lib;
  
      static std::function&lt;void(const bool &amp;)&gt; update1Delegate;
      static std::function&lt;void(const double &amp;)&gt; update2Delegate;
      static std::function&lt;void(const bool &amp;)&gt; update3Delegate;
  
      // Static methods to use in c-style callback but doesn't have access to class signals and member variables
      static void update1CB(uint8_t, uint8_t);
      static void update2CB(double, double);
      static void update2CB(uint8_t, uint8_t);
  
  };
  </code></pre>
  <pre><code>// hw_api_interface.cpp
  // Use static functions to call class member variables to have access to signals
  std::function&lt;void(const bool &amp;)&gt; hwi::update1Delegate{};
  std::function&lt;void(const double &amp;)&gt; hwi::update2Delegate{};
  std::function&lt;void(const bool &amp;)&gt; hwi::update3Delegate{};
  
  hwi::hwi() : m_lib(HWLIB::GetInstance())
  {
      update1Delegate = [&amp;](auto value) { emit status1Changed(value); };
      update2Delegate = [&amp;](auto value) { emit status2Changed(value); };
      update3Delegate = [&amp;](auto value) { emit status3Changed(value); };
  
      // Register call backs
      m_lib.SetUpdate1CB(update1CB);
      m_lib.SetUpdate2CB(update2CB);
      m_lib.SetUpdate3CB(update3CB);
  }
  
  void hwi::update1CB(uint8_t old_value, uint8_t new_value)
  {
      update1Delegate(static_cast&lt;bool&gt;(new_value));
  }
  
  void hwi::update2CB(double old_value, double new_value)
  {
      update2Delegate(new_value);
  }
  
  void hwi::update3CB(uint8_t old_value, uint8_t new_value)
  {
      update3Delegate(static_cast&lt;bool&gt;(new_value));
  }
  
  // void hwi::update3CB(uint8_t old_value, uint8_t new_value)
  // {
  //     Qt::postEvent(update1Changed, new_value); // new_value wrapped in QEvent?
  // }
  </code></pre>