Skip to main content
OSton.1
Associate
November 20, 2020
Solved

Issue with adding button in code. Am I doing this right?

  • November 20, 2020
  • 4 replies
  • 3702 views

Hello!

I am trying to add a button to my application manually through code. I thought it would be quite simple, but for some reason I am having some difficulties.

I have added it my screenView class, and initialised it using bitmapped images from the TouchGFX library.

It displays on screen correctly, but does not respond to any touches. I would expect at this point for the image on screen to change when I press it.

I have added other buttons from the designer, that are responding correctly.

Does anyone have any idea what I am doing wrong? Is there something I am not initialising correctly that links the button to TouchGFX engine?

I'm embarrassed to say that this has stumped me for several hours now!

Thank you.

screenView.hpp:

#ifndef SCREENVIEW_HPP
#define SCREENVIEW_HPP
 
#include <gui_generated/screen_screen/screenViewBase.hpp>
#include <gui/screen_screen/screenPresenter.hpp>
#include <touchgfx/widgets/Button.hpp>
 
class screenView : public screenViewBase
{
public:
 screenView();
 virtual ~screenView() {}
 virtual void setupScreen();
 virtual void tearDownScreen();
 
 touchgfx::Button Button1;
 
 
 
protected:
 
 
private:
 
 
 
};
 
#endif // SCREENVIEW_HPP

screenView.cpp:

#include <gui/screen_screen/screenView.hpp>
#include <BitmapDatabase.hpp>
 
screenView::screenView()
{
 
}
 
void screenView::setupScreen()
{
	screenViewBase::setupScreen();
 
	Button1.setXY(0, 0);
	Button1.setBitmaps(touchgfx::Bitmap(BITMAP_ROUND_EDGE_SMALL_ID), touchgfx::Bitmap(BITMAP_ROUND_EDGE_SMALL_PRESSED_ID));
 
	add(Button1);
}
 
void screenView::tearDownScreen()
{
 screenViewBase::tearDownScreen();
}

This topic has been closed for replies.
Best answer by Michael K

Hi @OSton.1​ . Sorry, I was unable to reproduce your issue. I started a new TouchGFX 1.14.0 project targetting the F469Disco 3.0.0 template, added a box background, a button and a slider. Then I copied your code. I got a compiler error and needed to change the bitmap set line to:

Button1.setBitmaps(touchgfx::Bitmap(BITMAP_BLUE_BUTTONS_ROUND_EDGE_SMALL_ID), touchgfx::Bitmap(BITMAP_BLUE_BUTTONS_ROUND_EDGE_SMALL_PRESSED_ID));

This reflects the bitmaps already generated for the blue button I added in the designer.

With this, in the simulator and on target the button appeared at 0,0 and the bitmap updated as it should when pressed. Perhaps consider uploading your project? Is it possible your touchscreen isn't calibrated, and it's not registering clicks in the area of 0, 0?

Edit: so that future readers don't have to scroll, an uncalibrated touchscreen was indeed the issue.

4 replies

Michael K
Senior III
November 20, 2020

Do you have any visible drawables (such as containers) that have the ClickListener mixin enabled that overlap your button? Doing so will intercept any clicks to elements below.

Embedded UI/UX Consulting: cadenza.design
OSton.1
OSton.1Author
Associate
November 21, 2020

I have one other button and a slider added through the designer. But niether of them have the ClickListener mixin enabled.

MM..1
Chief III
November 21, 2020

Add button isnt that simple. Better is add it in designer and set invisible.

OSton.1
OSton.1Author
Associate
November 21, 2020

Yeah normally I would use the designer, but I wanted to understand the process of adding widgets manually.

MM..1
Chief III
November 21, 2020

Then add in designer and read ViewBase generated code

Michael K
Senior III
November 21, 2020

This looks like the code that the designer generates. When you talk about adding a button and setting it invisible, you do this to ensure that the bitmap resource gets generated right? I can see a problem if you try using a bitmap that isn't generated, but wouldn't that be a compile error?

Ill try this out later today.

Embedded UI/UX Consulting: cadenza.design
Michael K
Michael KBest answer
Senior III
November 21, 2020

Hi @OSton.1​ . Sorry, I was unable to reproduce your issue. I started a new TouchGFX 1.14.0 project targetting the F469Disco 3.0.0 template, added a box background, a button and a slider. Then I copied your code. I got a compiler error and needed to change the bitmap set line to:

Button1.setBitmaps(touchgfx::Bitmap(BITMAP_BLUE_BUTTONS_ROUND_EDGE_SMALL_ID), touchgfx::Bitmap(BITMAP_BLUE_BUTTONS_ROUND_EDGE_SMALL_PRESSED_ID));

This reflects the bitmaps already generated for the blue button I added in the designer.

With this, in the simulator and on target the button appeared at 0,0 and the bitmap updated as it should when pressed. Perhaps consider uploading your project? Is it possible your touchscreen isn't calibrated, and it's not registering clicks in the area of 0, 0?

Edit: so that future readers don't have to scroll, an uncalibrated touchscreen was indeed the issue.

Embedded UI/UX Consulting: cadenza.design
Michael K
Senior III
November 21, 2020

See attached

Embedded UI/UX Consulting: cadenza.design