How to stop a thread when user close the window

I had started a thread in mainComponent, and I want to stop the thread if the user clicks the close button in mainWindow. I konw stop the thread by stopThread(), but don’t know how to send a signal or message from mainWindow’s closeButtonPressed() to mianComponent to call the function. I had tested the changeBroadcaster and actionBroadcaster to send the message, and it works very well in another components. But doesn’t work when sending message from mainWindow to mainComponent.
Does anyone knows why it doesn’t work?
I would appreciate it if anyone can give me some advices. Thanks in advance.

Should the thread be stopped only when the user actually clicks the close button, or when the application is being quit in general? Quitting could also happen by pressing ⌘Q / Alt-F4, or through a menu.

To handle the button click only: The window also has a getCloseButton() method. You could call that from your application class, and then Button::addListener().

To handle application quits in general: Stop the thread in your application class’s shutdown() method.

The thread should stopped only by clicking the close button in mainWindow.
This is Main.cpp

and this is MainComponent.cpp

Thanks you very much, I will try your way.

This seems may not send a signal or message to mainComponent, the mainComponent and mainWindow does not exist in a common .h header. Did i get it wrong?