I am using QT4 to develop an application that has two parts: a worker thread and a GUI. The threads job is to wait for data to be placed in a shared memory buffer. When data is available (indicated through the use of a blocking semaphore), the thread reads the data from the buffer, processes it, and needs to pass that data to the GUI for display.

In QT4 the way of using the QThread class has changed. It is no longer recommended to subclass QThread (as it is an interface to the thread, and not the thread itself). Rather, examples show the creation of a class (based on QObject) that will actually run in the thread and perform the work.

Given that… How do I communicate from the GUI to the thread? Do I (can I) post events from the GUI – and if so, do I post them to the instance of the QThread, or to the class I created that does the actual work inside the thread? I want the GUI to be able to tell the thread to stop its blocking operation and terminate (so that the application can close), but I cannot come up with a way to do that. Example code would be much appreciated!

FYI: I had already created this application under QT3 using a subclassed QThread that polls the semaphore and posts an event to the GUI when data is ready. The GUI uses its customEvent() function to respond to the event, and a nonblocking message queue to communicate with the thread (inside its run function.) I’d like to do this differently, if possible, under QT4.