cancel
Showing results for 
Search instead for 
Did you mean: 

Setting RadioButton state without triggering action.

franck23
Senior

Hi,

I have 3 radio-buttons in a custom container which is present on several screens.

I would like the radio-buttons to keep their states between when switching screens.

I have the radio-buttons states reported to the model every time a button is pressed and when the screen changes, the new active view fetches the radio-button state.

However, I do not see a function to update a radio-button state without triggering the button action.

Is there such a function?

16 REPLIES 16
MM..1
Chief III

Why you define callbacks in base code, when i create screen with radio my code is

/*********************************************************************************/
/********** THIS FILE IS GENERATED BY TOUCHGFX DESIGNER, DO NOT MODIFY ***********/
/*********************************************************************************/
#include <gui_generated/screen1_screen/Screen1ViewBase.hpp>
#include <touchgfx/Color.hpp>
#include "BitmapDatabase.hpp"
 
Screen1ViewBase::Screen1ViewBase()
{
 
    __background.setPosition(0, 0, 800, 480);
    __background.setColor(touchgfx::Color::getColorFrom24BitRGB(0, 0, 0));
 
    toggleButton1.setXY(336, 193);
    toggleButton1.setBitmaps(touchgfx::Bitmap(BITMAP_BLUE_TOGGLEBARS_TOGGLE_ROUND_LARGE_BUTTON_OFF_ID), touchgfx::Bitmap(BITMAP_BLUE_TOGGLEBARS_TOGGLE_ROUND_LARGE_BUTTON_ON_ID));
 
    radioButton1.setXY(356, 43);
    radioButton1.setBitmaps(touchgfx::Bitmap(BITMAP_BLUE_CHECK_BUTTONS_CHECK_MARK_INACTIVE_ID), touchgfx::Bitmap(BITMAP_BLUE_CHECK_BUTTONS_CHECK_MARK_PRESSED_ID), touchgfx::Bitmap(BITMAP_BLUE_CHECK_BUTTONS_CHECK_MARK_ACTIVE_ID), touchgfx::Bitmap(BITMAP_BLUE_CHECK_BUTTONS_CHECK_MARK_NORMAL_ID));
    radioButton1.setSelected(false);
    radioButton1.setDeselectionEnabled(false);
 
    radioButton2.setXY(356, 104);
    radioButton2.setBitmaps(touchgfx::Bitmap(BITMAP_BLUE_CHECK_BUTTONS_CHECK_MARK_INACTIVE_ID), touchgfx::Bitmap(BITMAP_BLUE_CHECK_BUTTONS_CHECK_MARK_PRESSED_ID), touchgfx::Bitmap(BITMAP_BLUE_CHECK_BUTTONS_CHECK_MARK_ACTIVE_ID), touchgfx::Bitmap(BITMAP_BLUE_CHECK_BUTTONS_CHECK_MARK_NORMAL_ID));
    radioButton2.setSelected(true);
    radioButton2.setDeselectionEnabled(false);
 
    add(__background);
    add(toggleButton1);
    add(radioButton1);
    add(radioButton2);
    radioButtonGroup1.add(radioButton1);
    radioButtonGroup1.add(radioButton2);
}
 
void Screen1ViewBase::setupScreen()
{
 
}

no callbacks here...

place this in view setup code after change selected states

radioButtonGroup.setRadioButtonSelectedHandler(radioButtonSelectedCallback);

franck23
Senior

The callback defined in the base file has been generated using TouchGFX designer.

The code I have added is in the MainView.cpp.

As i write when you plan change state against designer states, without call callback, then you can dont generate callbacks to base file, and do it yourself in setup code ...

maybe too is possible redefine callback to null , set new group state and reaply original callback, but this i dont try.

MPast.1
Senior II

Hi all,

I have the same problem (I'm using 4.24.0 version):

I have a lot of screens with radiobutton, And at at startup I need to change the selections due to user configuration.

But everytime I call "setSelected" the relative action is called and this trigger wrong behaviour on my code

I suggest in the next touchgfx release to insert a method similar to "forcestate" to upgrade the radiobutton state without generate action.

I would like to avoid to delete the callback in realtime and after change the status re-enabled them.

Hello,

@MPast.1, you are the latest comment in this thread about the double firing of the callback function, did you manage to solve this issue?
in my application I'm using the setSelected function of the radio button and not of the radio button group, I manage to call the callback function only once as i wanted but since it wasn't the setSeclted function of the radio button group it didn't felt right. if you solved this, it would be kind of you to share the solution.

Thank you and best regards

HI H_Drx ,
in my project the "setSelected" function was used to load the right
values at screen startup to show the right settings on the various
control, before the screen begin show.
(in my case, every radio button callbacks set a value ON/OFF into some
flags and sent a message with the updated value to another device by a
UART).
My trick was to block for 1 seconds the message out from the uart at the
screen startup, to allow the screen to load the value and draw the
update status on various screen objects.
A simple flag is set to ONE at screen startup, block the uart (for 1
second) and then is set to ZERO when time is elapsed.
All others interaction, when time is expired with the radio buttons are
correctly managed after this "simple startup mask system".
This choose was taken to made to be more flexible and more compatible
with new or old version of TouchGFX, avoid to edit the original
framework files.
This trick not solved the problem but fake for a moment the radiobuttons
interactions. (others object, doesn't call the relative callback when
you set the initial value, so this problem is not visible to all).
Hope this trick can help you. If you discover an improvement on the
management, please write to me). Thanks.
H_Drx
Associate II

Hi @MPast.1 
I believe by saying "setSelected", you mean the function in the radio button group, because that one fires the callback twice, but if you try to use the "setSelected" of the radio button (not the group), that will fire the callback only once.
and if you set a counter depends on how many things you have to configure and when the counter reached that number then you can send the UART, no timer needed here, well, this depends on your architecture for sure. and this didn't solve the original problem,
this is the "setSelected" function of the RadioButton group

 virtual void setSelected(RadioButton& radioButton)
    {
        radioButton.setSelected(true);
        radioButtonClickedHandler(radioButton);
    }

as you can see, it already call the "setSelected" function of the radio button itself, and also call the click handler that will set the select option to other radio button to false and then run a call back, that call back will also fire the same "setSelected" function of radio button that have been fired, so, this is why it fires twice.

 virtual void radioButtonClickedHandler(const AbstractButton& radioButton)
    {
        // Deselect other radio buttons
        for (uint16_t i = 0; i < size; i++)
        {
            if (radioButtons[i] != &radioButton)
            {
                if (radioButtons[i]->getSelected())
                {
                    radioButtons[i]->setSelected(false);
                }
            }
        }

        if (radioButtonSelectedCallback && radioButtonSelectedCallback->isValid())
        {
            radioButtonSelectedCallback->execute(radioButton);
        }
    }


I don't know what is the correct initialization i should do to make it fire only once. not sure if this is a bug or we shouldn't use the setSelect of radio button group at all. in the end, i just used the setSelect of each radio button for my dynamic configuration.