title: How to read HID devices (/dev/hidrawX) with Qt on Linux?
body: <p>I'm working on a 'RepRap calibration tool' which would use a mouse attached to the printing platform to measure the movement of the platform.</p>
  <p>Right now I'm stuck trying to read raw mouse data from <code>/dev/hidrawX</code>, but I'm unable to read any data.</p>
  <p>So far I've tried:</p>
  <h3>First attempt:</h3>
  <pre><code>QFile f(&quot;/dev/hidraw0&quot;);
  f.readAll();
  </code></pre>
  <p>Reads nothing.</p>
  <h3>Second attempt:</h3>
  <pre><code>m_file = new QFile(&quot;/dev/hidraw0&quot;);
  m_sn= new QSocketNotifier(m_file-&gt;handle(), QSocketNotifier::Read);
  m_sn-&gt;setEnabled(true);
  connect(m_sn, SIGNAL(activated(int)), this, SLOT(readyRead()));
  </code></pre>
  <p>then on the readyRead SLOT:</p>
  <pre><code>qDebug()&lt;&lt;&quot;Ready Read!!&quot;&lt;&lt;m_file.bytesAvailable();
  QTextStream d(&amp;m_file);
  qDebug()&lt;&lt; d.read(64);
  </code></pre>
  <p>This code fires the readyRead slot once but it gets stuck on the read(64) call, if I comment the read(64) the slot will be fired each time the mouse its moved.
  <code>m_file.bytesAvailable()</code> always reports 0.</p>
  <p>Which is the right way to read these devices with Qt?</p>
  <h1>Solution:</h1>
  <p>I reworked the code like:</p>
  <pre><code>bool rcMouseHandler::openHidraw(QString device)
  {
      int fd =open(device.toLocal8Bit(),O_NONBLOCK);
      if(fd &lt;=0)
      {
          qDebug()&lt;&lt;&quot;[WARN]rcMouseHandler::open-&gt; Cant open!&quot;;
          return false;
      }
      m_sn= new QSocketNotifier(fd, QSocketNotifier::Read);
      m_sn-&gt;setEnabled(true);
      connect(m_sn, SIGNAL(activated(int)), this, SLOT(readyRead()));
      return true;
  }
  
  void rcMouseHandler::readyRead()
  {
      qDebug()&lt;&lt;&quot;Ready Read!!&quot;;
      char buffer[4] = {0,0,0,0};
      read(m_sn-&gt;socket(),&amp;buffer,4);
      qDebug()&lt;&lt;(quint8)buffer[0]&lt;&lt;(quint8)buffer[1]&lt;&lt;(quint8)buffer[2]&lt;&lt;(quint8)buffer[3];
  }
  </code></pre>