Skip to main content
SYama.11
Associate II
July 8, 2020
Solved

How to unresister Click and Drag Event manually

  • July 8, 2020
  • 2 replies
  • 1629 views

Hi,

I'm trying to change screen with button on a screen and hardware button.

But, I don't want to add another screen because some parameter is shared,

So, I use pointers for some widgets and generate/delete the widgets at runtime.

However, when I press the trigger key to change screen during pressing the button on the screen and release the button after the screen has changed, the program has crashed.

I think that the program try to call "Button::handleClickEvent" of the button that has already deleted but failed, it has crashed.

Here's a some sample code.

class sampleScreen {
~~~~~~~~~~
 virtual void setupScreen();
~~~~~~~~~~
 void buttonPressed();
 void triggerKeyPressed();
 void switchScreen();
~~~~~~~~~~
 Button* button;
~~~~~~~~~~
}
 
sampleScreen::setupScreen() {
 button = new Button();
~~~~~~~~~~
 add(*button);
 getRootContainer().invalidate();
}
 
void sampleScreen::buttonPressed() {
 switchScreen();
}
 
void sampleScreen::triggerKeyPressed() {
 switchScreen();
}
 
void sampleScreen::switchScreen() {
 remove(*button);
 delete button;
 getRootContainer().invalidate();
}

​If there is a way to unresister to listen click event manually when the program switches screen.

Do you know how to do this ? Or should I change plan ?

This topic has been closed for replies.
Best answer by Alexandre RENOUX

Hello,

I'm sorry but I still don't understand exactly what is the application about.

Anyway, if you really want to remove the clickEvent from a widget (like a button), you need to implement the ClickEvent in your Screen, without calling the ClikcEvent of your ScreenBase. This way, only the ClickEvent in your screen will be executed and not the one from the widget.

In the ClickEvent function implemented in your Screen, you can have a flag that authorize the click from the widget in one case but not in other cases like something as follows :

void ScreenView::handleClickEvent(const ClickEvent& evt)
{
 if(buttonExist) // execute the button clickEvent function
 {
 ScreenViewBase::handleClickEvent(evt);
 }
 // Else do nothing
}

In the end, I still suggest to have different screens, because constantly removing and recreating widgets at runtime on the same screen can become quite a mess and not very stable in my opinion.

But if it works this way then this is what matters most ;)

/Alexandre

2 replies

Alexandre RENOUX
Visitor II
July 9, 2020

Hello,

You say you don't want to add another screen because some parameters are shared but you can actually share parameters between screens.

For that you need to save the parameters/variables in the Model. There's only one model in your application that is shared with all the screens.

So creating another screen would be a better and easier idea.

/Alexandre

SYama.11
SYama.11Author
Associate II
July 9, 2020

Hello Alexandre,

Thank you for your answer.

I understood to use the Model for parameters to save, but how about widgets like background images or something (e.g. clock, status icons...etc.)?

Should I save widget parameters (Color, BitmapId, TypedTextId, Position and Size...) in the Model and read values everytime entering the screen?

I don't like the idea because when the application gets large, the Model has so many parameters but they are not used in all screens.

Furthermore, I'm afraid that dividing screens sacrifices the readability of the code because those screens are no longer related. So I have to write same functions in the presenter of each screen.

What do you think?

Alexandre RENOUX
Visitor II
July 10, 2020

Hello,

"'I'm afraid that dividing screens sacrifices the readability of the code"

On the contrary actually, I think that having different screens helps dividing the code in different files therefore increasing the readability.

"So I have to write same functions in the presenter of each screen."

True to a certain extent.

I feel like (correct me if I'm wrong) that you want a kind of template. Like the backgrounds, icons, etc... must be shared.

In this case, you don't need to share all the parameters in the Model but only one variable that will tell which set of background, icons, etc. must be implemented for each screen. Depending on the value of the shared variable, the position, BitmapId, Size, etc. will be the same.

Also if you really have to save a significant number of parameters, you can store then in structures to ease the understanding and implement only a couple of functions.

I don't know exactly what you are trying to do, so a picture or something similar might be useful to have more insight on your project.

/Alexandre

SYama.11
SYama.11Author
Associate II
July 12, 2020

deleted​