0

I am trying to make my QSystemTrayIcon class a separate thread class by doing the following

  1. Derived my TrayIcon class from QThread
  2. Implemented the run() method in TrayIcon class
  3. Moved the TrayIcon Initialization code to Run method

call TryIcon.start() from the main.

But my application crashing while executing the "connect" statement in the TrayIcon class. The connect statements are for the trayIcon menu functionality.

I am trying to understand the QThread, connect functionality by reading the QT documentation, examples.

TimeRun Cit
  • 177
  • 1
  • 3
  • 10
  • 1
    GUI stuff must stay on the GUI (main) thread. Why are you trying to put tray icon handling on a separate thread? That doesn't really sound appropriate. – Mat May 08 '12 at 16:20
  • This is a duplicate/related question. See http://stackoverflow.com/questions/638251/how-to-emit-cross-thread-signal-in-qt – felixgaal May 08 '12 at 22:06

1 Answers1

0

You can't do that. All GUI code must reside in the GUI thread. If you want, you can implement time-consuming aspects of the tray icon behavior in a separate QObject. Then move that QObject to a separate thread and communicate with it from the GUI thread either via signal/slot connections (not direct slot calls!) or by posting events. Directly calling is slot is pointless: the slot code will execute in the calling thread, and that's not what you want.

If you want to make life easier, you can expose convenience slots on your QObject. Those are merely connected to the protected or private slots that do the real work. You'd establish such connection in the constructor of the QObject, and it must be a QueuedConnection. You can then call the convenience slots directly: behind the scenes, Qt will marshall the arguments and post them as an event in the event queue of the thread the QObject lives in. The event loop in that thread will pick them up from the queue and dispatch calls to the slots that do work in your QObject.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313