title: Qt Thread with ping operation in Linux
body: <p>I am trying to accomplish what looks like a very simple task... but it's being a nightmare instead.</p>
  
  <p>I developed an app for a Linux Laptop. Inside the app, I would like to have an independent thread that continuously pings another PC (eg once every 5 seconds, forever... well, as long as the laptop is switched on).</p>
  
  <p>Of course, when the PC that the app is pinging is not connected, the app has to work smoothly, without waiting for the ping operation to return... How can I achieve this?</p>
  
  <p>At first I used a <code>QTimer</code> with <code>QProcess:execute("ping"...)</code>, which works fine. The problem is that if the other PC doesn't reply, my whole app and its GUI freeze for about one second at each ping operation. Changing the "ping" options (setting "-i0.2", for example), in order to reduce waiting time for the ping operation, didn't help: when the other PC is not connected my app becomes very slow. If I remove the ping, of course everything works smoothly.</p>
  
  <p>So, I decided to insert the "ping" operation in a <code>QThread</code>, but when I try to follow the second example in <a href="http://doc.qt.io/qt-4.8/qthread.html" rel="nofollow">http://doc.qt.io/qt-4.8/qthread.html</a>, nothing seems to work: the app doesn't even start.</p>
  
  <p>Here's the code:</p>
  
  <pre><code>//Pinger.h
  class Pinger : public QThread
  {
  Q_OBJECT  
   void run();
  public:
      void setParam(const QString &amp;urlToPing); // it sets the url to ping
  
  signals:
      /// \brief  Signal emitted when pinging of specified url fails
      void pingFailed(int ok);
  private:
      QString pingurl;
  };
  
  //Pinger.cpp
  void Pinger::run()
  {
      int exitCode;
      QProcess pingProc;
      while(true)
      {
          exitCode=pingProc.execute("ping",QStringList() &lt;&lt; "-c 1" &lt;&lt; "-i0.2" &lt;&lt; pingurl);
          emit pingFailed(exitCode);
          sleep(5);
      }
  }
  
  // MainWindow::MainWindow
          pinga= new Pinger(); // defined in mainwindow.h as Pinger* Pinga
          pinga-&gt;setParam(ip_address);
          connect(pinga,SIGNAL(pingFailed(int)),this,SLOT(connectionLost(int)));
          connect(pinga,SIGNAL(finished()),pinga,SLOT(deleteLater()));
          pinga-&gt;start();
  </code></pre>
  
  <p>Has anyone tried anything similar? I am quite new to Qt, but this operation seems so trivial that I find it incredible that there's no clear way to implement it. so I hope I am just missing something obvious.</p>