Skip to main content
BParh.1
Senior III
June 30, 2021
Solved

How to setup callback for a Radio Button Group ?

  • June 30, 2021
  • 4 replies
  • 1460 views

So let say I have a group of radio button like this:

touchgfx::RadioButtonGroup<5> radioButtonGroup1;

And whenever use select every radio button inside the group, I want to trigger the same callback handler.

How can I achieve this?

'

This topic has been closed for replies.
Best answer by BParh.1

Ok finally I got it:

//in .hpp
touchgfx::Callback<MainView, const touchgfx::AbstractButton&> cbRadioBtnGroup;
void cbRadioBtnGroupHandler(const touchgfx::AbstractButton& src);
 
 
// in .cpp
MainView::MainView() :
 cbRadioBtnGroup(this, &MainView::cbRadioBtnGroupHandler) {
 radioButtonGroup.setRadioButtonSelectedHandler(cbRadioBtnGroup);
}
 
MainView::
void MainView::cbRadioBtnGroupHandler(const touchgfx::AbstractButton& src) {
 if(&src == &radioButton1) {
 // handle for radioButton1 selection
 }
 else if(&src == &radionButton2) {
 // handle for radioButton2 selection
 }
 ....
 else {}
 
 
}

4 replies

ktrofimo
Senior III
June 30, 2021

It looks like only individual callbacks from every button are available​

BParh.1
BParh.1Author
Senior III
June 30, 2021

If that is the case, then the code could be really messy, as I have a lot radio buttons and I don't want each of radio button has separate callback.

But I think it is possible to have a general callback for a group of radio button, since I see class RadioButtonGroup has public member function

voidsetRadioButtonSelectedHandler(GenericCallback< const AbstractButton & > & callback)

https://support.touchgfx.com/docs/api/classes/classtouchgfx_1_1_radio_button_group#public-functions

But, it just I dont know how to use it right

BParh.1
BParh.1Author
Senior III
June 30, 2021

so far I did this way but it has build error:

in .hpp

Callback<HomeView, const RadioButtonGroup<5>&, const ClickEvent> cbRadioBtn;
 
void cbRadioBtnHandler(const RadioButtonGroup<6>& src, const ClickEvent ev) {
 
 // handle selected radio button
 
}

in constructor:

HomeView::HomeView():
 cbRadioBtn(this, &HomeView::cbRadioBtnHandler) {
 
}
 
HomeView::setupScreen() {
 
 radioButtonGroupParamsList.setRadioButtonSelectedHandler(cbRadioBtn);
}

But it has compile errror:

Severity Code Description Project File Line Suppression State

Error (active) E0434 a reference of type "touchgfx::GenericCallback<const touchgfx::AbstractButton &, void, void> &" (not const-qualified) cannot be initialized with a value of type "touchgfx::Callback<HomeView, const touchgfx::RadioButtonGroup<(uint16_t)6U> &, const touchgfx::ClickEvent, void>" Application C:\TouchGFXProjects\LagrevSimulation3\gui\src\home_screen\HomeView.cpp 41

Error C2664 'void touchgfx::RadioButtonGroup<6>::setRadioButtonSelectedHandler(touchgfx::GenericCallback<const touchgfx::AbstractButton &,void,void> &)': cannot convert argument 1 from 'touchgfx::Callback<HomeView,const touchgfx::RadioButtonGroup<6> &,const touchgfx::ClickEvent,void>' to 'touchgfx::GenericCallback<const touchgfx::AbstractButton &,void,void> &' Application c:\touchgfxprojects\lagrevsimulation3\gui\src\home_screen\homeview.cpp 41

BParh.1
BParh.1AuthorBest answer
Senior III
July 1, 2021

Ok finally I got it:

//in .hpp
touchgfx::Callback<MainView, const touchgfx::AbstractButton&> cbRadioBtnGroup;
void cbRadioBtnGroupHandler(const touchgfx::AbstractButton& src);
 
 
// in .cpp
MainView::MainView() :
 cbRadioBtnGroup(this, &MainView::cbRadioBtnGroupHandler) {
 radioButtonGroup.setRadioButtonSelectedHandler(cbRadioBtnGroup);
}
 
MainView::
void MainView::cbRadioBtnGroupHandler(const touchgfx::AbstractButton& src) {
 if(&src == &radioButton1) {
 // handle for radioButton1 selection
 }
 else if(&src == &radionButton2) {
 // handle for radioButton2 selection
 }
 ....
 else {}
 
 
}