Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

What is the recommended way for a lower level software component/module to communicate with the GUI? I'm using C++.

I have a service layer class that if some conditions occur needs to notify the user, but still continue processing. In the case of an unrecoverable error I understand that I can use exceptions to bubble up to the GUI, but what about in the case of a simple message that needs to be communicated to the user where the lower level component continues executing?

UPDATE

Just to clarify the service layer (aka business logic, compute layer, etc) does not "know" about the GUI nor do I want it to.

share|improve this question
add comment

4 Answers

Depends on if the compute layer 'knows' about the gui.
If the compute layer can use the gui toolkit then you can post windows custom events or send Qt signals or whatever your gui toolkit uses.

If you have more separation, eg. your compute thread is written in pure c and doesn't have any gui hooks, then you typically either supply a 'c' callback function from the gui that the compute thread calls. If the compute thread is a separate process or you are monitoring many of them then some IPC mechanism like a named pipe or in the worst case your compute thread writes a status file that the gui periodically checks for,

share|improve this answer
    
The compute layer does not know about the GUI. –  User Mar 8 '12 at 19:42
    
@User then callbacks are simplest –  Martin Beckett Mar 8 '12 at 20:30
add comment

I'd use an event queue or something similar. Your business logic layer can simply push a "something broke" message onto the queue and any other layers that are monitoring that queue can take whatever action is appropriate for them.

share|improve this answer
    
I like this idea. Any articles on how to do this in C++? –  User Mar 8 '12 at 22:19
    
I'm not really a C++ guy, but here's one that looks appropriate. –  TMN Mar 9 '12 at 12:47
add comment

It depends what type & quantity of data are you using:


Example 1.

Showing one line of text, at a time, from a text file or table, on a text control.

Example 2.

Showing a single record / entity from a table, in a panel with several controls, in which each control is a property or field from an entity. The entity has several fields or properties.

Example 3

Showing a list of entities / records, in a control, like list, treeview or gridview.


Example 1.

You may have a function in your app., where another function is called, with a single reference variable, where the value is going to be stored.

Example 2.

You may have a function in your app., where another function is called, with a single reference variable, where, the variable may be a pointer / reference to a "struct" or "object" that the values are going to be stored.

Example 3.

For example 3, there are 2 ways.

(3.1) you may have a function in your app., where another function is called, with a single reference variable, where the variable is collection / list / open array, where each element is an entity or object.

(3.2) you may have a function in your app., where a function is called, in a loop, each time, a single reference variable, where, the variable may be a pointer / reference to a "struct" or "object" that the values are going to be stored. Each time the loop is called the recovered object is displayed in the G.U.I., or added to a collection, and, later, displayed in the G.U.I.

Cheers.

share|improve this answer
add comment

You can try a signals-slots library:

If you don't yet have a preference for a GUI library, you can also try Qt. It has a very flexible and powerful signals-slots system as well. A minor downside is that Qt uses a custom preprocessor, moc, so it's not 100% C++.

share|improve this answer
    
Using MFC so can't do Qt. Have used Boost.Signals before so I may go this route. Is there a way to have the events/signals not block the business layer? –  User Mar 9 '12 at 19:10
    
In general, don't do anything blocking in the GUI thread. This is not specific to signal and slots, any other model behaves the same. Emit your signals to update the gui contents and that's all. If you need to trigger a blocking action from the gui, then do it in a thread (but if you are using signals, you can connect a worker from another thread to the same signal that triggers the gui update). –  Tamás Szelei Mar 9 '12 at 19:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.