title: Qt with ZeroMQ publish subscribe pattern
body: <p>I would like to use ZeroMQ(4.1.2) with Qt (5.2.1).
  Idea is to have zmq pub/sub (where server is outside) and sub is qt app.
  Currently receive in Qt app runs once, could someone drop hint? 
  Should ZeroMQ receiver be implemented in some other way?</p>
  
  <p>Currently my code looks like:</p>
  
  <p><strong>mainwindow.h</strong></p>
  
  <pre><code>namespace Ui {
  class MainWindow;
  }
  
  class MainWindow : public QMainWindow
  {
  Q_OBJECT
  
  public:
      explicit MainWindow(QWidget *parent = 0);
      ~MainWindow();
  
  private slots:
      void on_pushButton_clicked();
  
      void readZMQData();
  
  private:
      Ui::MainWindow *ui;
      QSocketNotifier *qsn;
      void *context;
      void *subscriber;
  
  };
  </code></pre>
  
  <p><strong>mainwindow.cpp</strong></p>
  
  <pre><code>MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
  {
      ui-&gt;setupUi(this);
  
      /***** ZMQ *****/
  
      context = zmq_ctx_new ();
      subscriber = zmq_socket (context, ZMQ_SUB);
      int rc = zmq_connect (subscriber, "tcp://localhost:5556");
  
      char *filter = "";
      rc = zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE,filter, strlen (filter));
      unsigned int fd=0;
      size_t fd_size = sizeof(fd);
      rc = zmq_getsockopt(subscriber,ZMQ_FD,&amp;fd,&amp;fd_size);
  
      qsn = new QSocketNotifier(fd, QSocketNotifier::Read, this);
      connect(qsn, SIGNAL(activated(int)), this, SLOT(readZMQData()), Qt::DirectConnection);
  
  }
  
  MainWindow::~MainWindow()
  {
      zmq_close (this-&gt;subscriber);
      zmq_ctx_destroy (this-&gt;context);
      delete ui;
  }
  
  
  void MainWindow::readZMQData()
  {
      qsn-&gt;setEnabled(false);
      qDebug() &lt;&lt; "Got data!";
  
      int events = 0;
      std::size_t eventsSize = sizeof(events);
      zmq_getsockopt(subscriber,ZMQ_EVENTS, &amp;events, &amp;eventsSize);
      if(events &amp; ZMQ_POLLIN){
          qDebug() &lt;&lt; " ======  Data to read ======";
  
          char *string = s_recv(subscriber);
          qDebug() &lt;&lt; "DATA: " &lt;&lt; string;
          free(string);
      }
  
      qsn-&gt;setEnabled(true);
  }
  </code></pre>
  
  <p><strong>And server app is (from ZeroMQ examples):</strong></p>
  
  <pre><code>#include "zhelpers.h"
  
  int main (void)
  {
      //  Prepare our context and publisher
      void *context = zmq_ctx_new ();
      void *publisher = zmq_socket (context, ZMQ_PUB);
      int rc = zmq_bind (publisher, "tcp://*:5556");
      assert (rc == 0);
  
      //  Initialize random number generator
      srandom ((unsigned) time (NULL));
      while (1) {
          //  Get values that will fool the boss
          int zipcode, temperature, relhumidity;
          zipcode     = randof (100000);
          temperature = randof (215) - 80;
          relhumidity = randof (50) + 10;
  
          //  Send message to all subscribers
          char update [20];
          sprintf (update, "%05d %d %d", zipcode, temperature, relhumidity);
          s_send (publisher, update);
      }
      zmq_close (publisher);
      zmq_ctx_destroy (context);
      return 0;
  }
  </code></pre>