4

When I open my application then the application is waiting for a connection to the server, I have done that by calling a slot run() which waits for a acknowledgement packet from server and when it receives it then it hides "Waiting for connection" string and loads other things. The problem is that when it waits for a packet then the system tray icon is not responding to anything, when the server sends packet and application loads then the system tray icon starts responding (for right-click menu).

I am using ZeroMQ for IPC.

I have something like this:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show(); 

    //THIS PART
    QTimer::singleShot(2000,&w,SLOT(run()));

    return a.exec();
}
user3840048
  • 141
  • 1
  • 9
  • Do you really need the timer, or is it just a crutch to invoke `run` from the event loop? You could use a zero-time single shot, it's OK. You don't need to "give time" to anything to complete. – Kuba hasn't forgotten Monica Aug 26 '14 at 18:10

2 Answers2

3

You block the event loop. This won't ever work. Unfortunately, ZMQ doesn't offer any platform-specific message loop integration. Thus you have to use it in a separate thread.

This is much easier since it's bad design anyway to put networking code in a widget class.

Create a ZMQ object that encapsulates your networking, and push it to a separate thread. As long as all your communication with that ZMQ instance is over signals/slots or QMetaObject::invokeMethod, you'll be OK.

See this answer for code to Thread.

class ZMQ : public QObject {
  Q_OBJECT
  Q_SLOT void run() {
    ...
    forever {
      socket.send(request,0);
      socket.recv(&response);
      if(response.compare("login") == 0) {
        emit loggedIn();
        socket.close();
        return;
      }
    }
  }
public:
  ZMQ() {}
  Q_SIGNAL void loggedIn();
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    ZMQ zmq;
    Thread thread; // see https://stackoverflow.com/a/25230470/1329652
    MainWindow w;

    w.connect(&zmq, SIGNAL(loggedIn()), SLOT(loggedIn()));
    zmq.moveToThread(&thread);
    thread.start();

    QMetaObject::invokeMethod(&zmq, "run");

    w.show(); 
    return a.exec();
}
Community
  • 1
  • 1
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
0

You need to run the code currently in Mainwindow::run() in a separate thread for it to not block the event loop.

Make a new QObject with a run slot containing the same code and move it to its own thread. Something like this:

QThread *thread = new QThread;
thread->start();
qobject_with_run_slot->moveToThread(thread);
QTimer::singleShot(2000, qobject_with_run_slot, SLOT(run()));
Kimmeh
  • 991
  • 7
  • 21