2020-11-20 08:53 AM
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();
}
Solved! Go to Solution.
2020-11-21 07:25 AM
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.
2020-11-20 03:03 PM
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.
2020-11-21 02:23 AM
Add button isnt that simple. Better is add it in designer and set invisible.
2020-11-21 05:25 AM
I have one other button and a slider added through the designer. But niether of them have the ClickListener mixin enabled.
2020-11-21 05:27 AM
Yeah normally I would use the designer, but I wanted to understand the process of adding widgets manually.
2020-11-21 05:36 AM
Then add in designer and read ViewBase generated code
2020-11-21 06:27 AM
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.
2020-11-21 06:35 AM
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.
2020-11-21 06:39 AM
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.
2020-11-21 07:01 AM
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.