Skip to main content
amirparto
Associate II
August 7, 2021
Solved

How to Instantiate a TouchGFX object dynamically?

  • August 7, 2021
  • 3 replies
  • 1700 views

Hi, I use TouchGFX in my HMI project, I want to create an instance of an object (ig. a button) in runtime (dynamically), so I could destroy it later and free it's allocation memory.

I tried create it in a method but it seems to have runtime error:

void Screen1View::create_button()
{
	touchgfx::Button button1;
	button1.setXY(0, 0);
	button1.setBitmaps(touchgfx::Bitmap(BITMAP_BLUE_BUTTONS_ROUND_EDGE_SMALL_ID), 
 touchgfx::Bitmap(BITMAP_BLUE_BUTTONS_ROUND_EDGE_SMALL_PRESSED_ID));
 
	add(button1);
 
	button1.invalidate();
}

0693W00000D1nskQAB.jpg 

Does anyone have any solution?

This topic has been closed for replies.
Best answer by scottSD

To my knowledge it can't be done. I asked about this a while back and i believe the answer has something to do with the way TouchGFX is configured.

You will need to place the declaration of that button in your class header file and it should work fine.

Here is @Martin KJELDSEN​  's explanation on this in the following thread:

https://community.st.com/s/question/0D50X0000AVS2tcSQD/is-it-possible-to-dynamically-add-guiobjects

3 replies

scottSD
scottSDBest answer
Senior III
August 9, 2021

To my knowledge it can't be done. I asked about this a while back and i believe the answer has something to do with the way TouchGFX is configured.

You will need to place the declaration of that button in your class header file and it should work fine.

Here is @Martin KJELDSEN​  's explanation on this in the following thread:

https://community.st.com/s/question/0D50X0000AVS2tcSQD/is-it-possible-to-dynamically-add-guiobjects

scottSD
Senior III
August 9, 2021
amirparto
amirpartoAuthor
Associate II
August 31, 2021

Thanks. It helped a lot.