Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
New Developer
viniciuslfta
Posts: 3
Registered: ‎04-03-2013
My Device: BlackBerry 10 dev alpha B
My Carrier: none
Accepted Solution

How to get the coordinate of the screen when touched.

Hello everyone!

 

I'm a beginner at blackberry development, also a beginner at programming in general ^^

 

I've made a few app's using just QML, and take some C++ tutorials

 

I'm trying to get the coordinate of screen when the user touches it, as I wasn't able to do that using just QML, so I found the source TouchEvent and tried to make it work on C++, to get the windowX and windowY coordinates of the touch.

http://developer.blackberry.com/cascades/reference/bb__cascades__touchevent.html

 

I'm using the image on background to try getting the event touch.
So far, it's:

HPP:

class LostInSpace : public QObject
{
    Q_OBJECT
public:
    LostInSpace(bb::cascades::Application *app);
    virtual  ~LostInSpace() {}
public slots:
Q_INVOKABLE float screentouchY(bb::cascades::TouchEvent* event);
Q_INVOKABLE float screentouchX(bb::cascades::TouchEvent* event);


private:
QPointer<bb::cascades::ImageView> fundo;

};

 CPP:

LostInSpace::LostInSpace(bb::cascades::Application *app)

{
	
	QmlDocument *qml =
QmlDocument::create("asset:///main.qml").parent(this);
	qml->setContextProperty("app", this);
	AbstractPane *root = qml->createRootObject<AbstractPane>();

	app->setScene(root);

	fundo = root->findChild<ImageView*>("fundo");
	bool res = QObject::connect(fundo, SIGNAL(touch(bb::cascades::TouchEvent*)),this, SLOT(screentouchY(bb::cascades::TouchEvent*)));
	Q_ASSERT(res);
	res = QObject::connect(fundo, SIGNAL(touch(bb::cascades::TouchEvent*)),
this, SLOT(screentouchX(bb::cascades::TouchEvent*)));
	Q_ASSERT(res);

}

float LostInSpace::screentouchY(bb::cascades::TouchEvent* event) {
	return event->windowY();
}
float LostInSpace::screentouchX(bb::cascades::TouchEvent* event) {
	return event->windowX();
}

 QML:  (I'll do the animations later, this is just to see if it work. I'm getting a "unknown simbol app" at the onTouch, I tought I just had to put "qml->setContextProperty("app", this);"  at the constructor)

ImageView {
                objectName: "fundo"
                imageSource: "images/fundo1.png"
                touchPropagationMode: TouchPropagationMode.Full
                onTouch: {
                    tiro.translationX = app.screentouchX();
                    tiro.translationY = app.screentouchY();
                }
            }


Any help will be much appreciated! 

Thank you!

 

 

Please use plain text.
Developer
jalue
Posts: 196
Registered: ‎03-04-2013
My Device: BB Z10
My Carrier: Rogers

Re: How to get the coordinate of the screen when touched.

Hey there,

 

You don't need to do that.. You can access the TouchEvent 'event' object within the onTouch: { } area of QML...

 

ImageView {
                objectName: "fundo"
                imageSource: "images/fundo1.png"
                touchPropagationMode: TouchPropagationMode.Full
                onTouch: {
                    tiro.translationX = event.windowX
                    tiro.translationY = event.windowY
                }
            }

 

Please use plain text.
New Developer
viniciuslfta
Posts: 3
Registered: ‎04-03-2013
My Device: BlackBerry 10 dev alpha B
My Carrier: none

Re: How to get the coordinate of the screen when touched.

Sorry, I marked as solution, but i'm still getting the error "Unknown symbol event"

Any idea?
Please use plain text.
Developer
jalue
Posts: 196
Registered: ‎03-04-2013
My Device: BB Z10
My Carrier: Rogers

Re: How to get the coordinate of the screen when touched.

You only have access to 'event' within the onTouch: { } area.. be sure you are using it there..
Please use plain text.