cancel
Showing results for 
Search instead for 
Did you mean: 

Create elements with code

ilyus
Senior II

Win 10, TouchGFX 4.16.1 Cube 1.6.1

Disco 469 Board with 800x480 DSI touchscreen

I have created a TouchGFX project and imported it into Cube (with the help of the video of EE by Karl youtube channel, since it's not an easy and obvious path).

Now, I've created a pair of buttons and stuff in TouchGFX, found the generated code in ScreenRunViewBase.cpp (ScreenRun is the name of the screen with buttons). It's creating and adding buttons over there, setting their colors and coordinates and stuff, looks easy.

The question is: I want to add buttons with my own code. Let's say I want to create a new array of buttons (or pointers to the button objects or whatever other way I can make a listed structure of them). Now, unfortunately, in the top of the file ScreenRunViewBase.cpp there is a message THIS FILE IS GENERATED BY TOUCHGFX DESIGNER, DO NOT MODIFY.

So how am I supposed to modify the code then? When the screen opens (I know there is trigger for that), how do I actually CREATE a new button/container/whatever? in what file of the program?

I tried to call ScreenRunViewBase::Pin1.setPosition(0, 0, 160, 100);

("Pin1" is a container element) from the main.c, but it shows syntax error. Replacing ScreenRunViewBase with touchgfx:: didn't help - same thing.

Pin1 itself is declared in protected section in ViewBase hpp. Even if I can't access it from the outside, I would still want to create my own element that I could access from anywhere (or just access at all, unlike the protected element).

Unfortunately, TouchGFX documentation shows a lot of code it generates, but it's not very useful given I can't modify it since it will rewrite all my changes next time I generate the files again.

So I want to write commands for creating elements that will be a) accessible to me via code to manipulate b) will stay there even if I generate files again (like cubemx).

I want a loop that creates 10 (2, 20, 1000) buttons and that I can loop through to set/change their properties. How can I achieve that?

1 ACCEPTED SOLUTION

Accepted Solutions
Alexandre RENOUX
Principal

Hello ilyus,

I recommend you to have a look at this page from the documentation website.

Also feel free to have a look at the UI Examples available in TouchGFX Designer. It should help you understand how a TouchGFX project is structured.

Creating a list of button is pretty straight-forward as it follows the coding rules of C++.

  • First you must declare an array of buttons. ex : Button myButtons[20]
  • Then for the initialization you usually do it in your ScreenView in the setupScreen() function or in the ScreenView constructor with a for loop.

ex:

for(int i = 0; i < 20; i++)
{
    myButtons[i].setPosition(x, y, width, height);
    myButtons[i].setImage(...);
   ....
}

/Alexandre

View solution in original post

3 REPLIES 3
MM..1
Chief II

Maybe little more time to study and you found , that view is generated from two parts, BASE is generated and for you read only when you plan next generation.

Second part is your files and here you can do what you need. This file is named and placed without generated prefix. For example

projectplace\TouchGFX\gui\include\gui\screen1_screen\Screen1View.hpp 

projectplace\TouchGFX\gui\src\screen1_screen\ Screen1View.cpp 

Alexandre RENOUX
Principal

Hello ilyus,

I recommend you to have a look at this page from the documentation website.

Also feel free to have a look at the UI Examples available in TouchGFX Designer. It should help you understand how a TouchGFX project is structured.

Creating a list of button is pretty straight-forward as it follows the coding rules of C++.

  • First you must declare an array of buttons. ex : Button myButtons[20]
  • Then for the initialization you usually do it in your ScreenView in the setupScreen() function or in the ScreenView constructor with a for loop.

ex:

for(int i = 0; i < 20; i++)
{
    myButtons[i].setPosition(x, y, width, height);
    myButtons[i].setImage(...);
   ....
}

/Alexandre

I have successfully created my own array of boxes that change colors on button click. Added a public array of fixed length, looped through it, provided coordinates calculated in the loop. Now I can finally generate everything the way I want a lot quicker and with a lot less pain now that I can loop through stuff and automate changes.

Thank you for showing me in the right direction, work is in progress!