cancel
Showing results for 
Search instead for 
Did you mean: 

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

OSton.1
Associate II

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();
}

1 ACCEPTED SOLUTION

Accepted Solutions
Michael K
Senior III

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.

View solution in original post

15 REPLIES 15
Michael K
Senior III

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.

MM..1
Chief

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

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

OSton.1
Associate II

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

Then add in designer and read ViewBase generated code

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.

No - all bitmaps is generated. But for objects as button you need write click listener callbacks function, that generator do for you. Without this button exist and show, but do nothing.

Really? Isn't the bitmap swapping code contained in the button widget? The callbacks are just if you want to do something in the view context in response to to the button press. The OP isn't even getting the bitmap swap.

Yes maybe you have right , but when you plan use images from touch buttons you need add one , when you use own images you can create it as you describe and images i mean must have same size , then click seems work.

I test it add one button in designer and second with your code and it both works without trouble.