Hello all,

I have a question about coupling C++ classes to a GUI, in a nice Object Oriented solution.

The problem is as follows:
I have designed a GUI application in a separate class Gui for instance, in a other source file I designed a hierarchical statemachine for my GUI application.
Now has my Gui class a serial port interface object in it as pointer member, when data is received by the serial port a event is raised and a function is called.
This function calls the statemachine, but in the statemachine I want to make changes in the Gui class based on the state, for intstance refreshing the screen or showing a dialog.

Whats a good solution to couple these two classes??


Marcel

how did you write the gui? win32 api, MFC, wxWindows or something else? And is the serial port code in the same or different thread than the gui?

I use a win32 api, with only 1 thread.

I looked on the internet and I found the Model View Controller pattern is this something what is normally used for this kind of problem?

You should create another thread and put that serial port stuff in it so that it doesn't block the gui main thread. Then when something comes in collect the string(s) and send a windows message to the main gui thread so that it can display the string(s) or do something else with them.

I am not very familiar with threads, but the threads are not the main problem, i think.
The main problem is the communication between the Gui class and the statemachine class. In the past I programmed the functions for my statemachine into the Gui class then the statemachine is in the same namespace as my Gui class and it can therefore access all components of my GUI. The serial port works then fine with only one thread.

But I want to decouple this, so that I can reuse my statemachine.

There must be a very common solution for this, or not??

in MS-Windows the two classes could communicate one class sending a private message to the other via win32 api SendMessage(). The gui class would process the message just like it processes all other windows mesages.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.