cancel
Showing results for 
Search instead for 
Did you mean: 

How to make image widget clickable?

LXGMAX
Visitor

I have placed several Image widgets in my Swipe Container, now I want the image widget to support clicks and call my function, how can I make it?

I have tried to inherit from the Image widget and implement handleClickEvent, however it doesn't implement the click event that I want.

My code:

#include <touchgfx/widgets/Image.hpp>
#include <touchgfx/Callback.hpp>
#include <touchgfx/Utils.hpp>

class ClickableImage : public Image {
public:
    ClickableImage() : Image(), clickAction(0) {}
    
    virtual void handleClickEvent(const ClickEvent& event) {
        touchgfx_printf("img click\n"); // Not run
        if (event.getType() == ClickEvent::PRESSED) {
           
            if (clickAction && clickAction->isValid()) {
                clickAction->execute(*this, event);
            }
        }
        Image::handleClickEvent(event);
    }
    
    void setClickAction(GenericCallback<const ClickableImage&, const ClickEvent&>& callback) {
        clickAction = &callback;
    }
    
private:
    GenericCallback<const ClickableImage&, const ClickEvent&>* clickAction;
};

 

1 REPLY 1
MM..1
Chief III

Activate clickable on images, and create func in screen 

void Screen4View::handleClickEvent(const ClickEvent& evt)
{
 if(evt.getType()==ClickEvent::PRESSED && image.getAbsoluteRect().intersect(evt.getX(), evt.getY()))
...