Linking UI to external object.

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
akhneyzar
In need of some credit
In need of some credit
Posts: 5
Joined: Wed Apr 21, 2021 2:51 pm
Location: France, Switzerland

Linking UI to external object.

Post by akhneyzar »

I am trying to have my GUI as decoupled as possible form my implementation.

I want the GUI to run independently from the MainProgram.

But as I understand I still need the (MainFrame : wxFrame) to know about the object that will transfer events form the UI to the MainProgram.

Are there some sample examples about this?
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Linking UI to external object.

Post by doublemax »

I'm not quite sure what you're looking for. In the most basic example you have a frame with a single button and an event handler for the button click. In the event handler you just call a method of your gui-less core.
Use the source, Luke!
BenKissBox
Earned a small fee
Earned a small fee
Posts: 13
Joined: Thu Jan 30, 2020 2:22 pm
Location: France / Netherlands

Re: Linking UI to external object.

Post by BenKissBox »

Hi Akneyzar,

please describe your problem more in detail. What implementation are you talking about ?
What is your platform ?

You can perfectly write a program which keeps the GUI processing completely separated from other processing. I suspect that you want to write your code with the "standard" C approach, with a main() function being the main "program" (what you call "implementation")

If it is the case, this is a very, very bad idea :cry:
You can not simply say to wxWidgets "please stay away of my main program". Any wxWidgets application need to be initialized in a specific way and if you want to follow another path, the application will either never start the GUI or will crash.

If you want to keep your "main" processing completely separated from the GUI, the best thing is use a thread. Write your "implementation" in a separate thread and let wxWidgets run in its own thread. The operating system will then do its work to have your thread appear as "running alone"
akhneyzar
In need of some credit
In need of some credit
Posts: 5
Joined: Wed Apr 21, 2021 2:51 pm
Location: France, Switzerland

Re: Linking UI to external object.

Post by akhneyzar »

The project is on Windows with C++. I am originally from embedded systems (C/Linux) so I might have to update my way of doing. I was used to have the UI run in another process and communicate to it through a socket.

My initial idea was to have the GUI in a thread launched by a 'WinMain' funcion (I already have something running), and have all the state machine and processing (camera feeds and image transformation) done in other threads.

So my first step was to integrate the UI with a StateMachine.
I added my StateMachine object as a parameter to my

Code: Select all

 MainFrame() : wxFrame () 
so that when an event happens the MainFrame could call the object function.

I had some difficulties doing so and my current idea is to have my MainFrame inherit wxFrame and StateMachine.

Code: Select all

MainFrame : public wxFrame, public StateMachine.
I was wondering if I'm on a complicated path.
I looked at the sample codes but I'm a bit lost as to which one could explain a good code architecture for a system rendering camera feeds.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Linking UI to external object.

Post by doublemax »

My initial idea was to have the GUI in a thread launched by a 'WinMain' funcion (I already have something running), and have all the state machine and processing (camera feeds and image transformation) done in other threads.
Sounds good.
I was used to have the UI run in another process and communicate to it through a socket.
The same principle should work with different threads. Use the event system to send information from the worker thread(s) to the GUI thread, use wxMessageQueue in the other direction. (Events are always processed in the GUI thread, so you can't send an event to a secondary thread).

https://docs.wxwidgets.org/trunk/classwx_thread.html
https://docs.wxwidgets.org/trunk/classw ... _01_4.html
I had some difficulties doing so...
Please give an example.
Use the source, Luke!
akhneyzar
In need of some credit
In need of some credit
Posts: 5
Joined: Wed Apr 21, 2021 2:51 pm
Location: France, Switzerland

Re: Linking UI to external object.

Post by akhneyzar »

The sample principle should work with different threads. Use the event system to send information from the worker thread(s) to the GUI thread, use wxMessageQueue in the other direction. (Events are always processed in the GUI thread, so you can't send an event to a secondary thread).

https://docs.wxwidgets.org/trunk/classwx_thread.html
https://docs.wxwidgets.org/trunk/classw ... _01_4.html


Thanks for the explanations I'll check.

I suspect that there might not be any sample showing this. Right?
I had some difficulties doing so...
Please give an example.
Ohh it's an exception occuring in the StateMachine code but our debug tooling has some issues we need to solve for investigating.
So we need to work on that.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Linking UI to external object.

Post by doublemax »

I suspect that there might not be any sample showing this. Right?
Not exactly. The "thread" sample contains the fundamentals you need, but as it demonstrates many things, it's relatively complex and not so easy to follow.
Use the source, Luke!
Post Reply