Gestures and QGraphicsScene
Hello,
I want to use gesture framework, but at the begining I have some problems.
I have QGraphicsView and QGraphicsScene
view->addScene(myScene);
At the myScene threre are few icons(QPixmapItem) . I try recognize QSwipeGesture( click somewhere on gaphicsScene and drag) and after recognizon start some animation (QtKinetic)
I have done:
Code:
view->grabGesture(Qt::SwipeGesture);
I had hoepe that event would propagate to QGraphicsScene and i have done in myScene
Code:
virtual bool myScene
::event(QEvent *event
) {
if (event
->type
() == QEvent::Gesture) do something
}
unfortunaltely program does not go to line "do something".
What I do wrong??
Re: Gestures and QGraphicsScene
First of all you should be grabbing the gesture on the view's viewport, not on the view itself.
Re: Gestures and QGraphicsScene
I modified my code. Now I run grab gestures on viewport and I try to catch QGesture event on object that inherits QGraphicsObject. Unfortunatelly it seems that there doesn't apper any gestureEvent. I have code everithing similar to example given by Qt.
I found the same problem in this and another forum, but there isn't any solve. So maybe is here anyone who menage to run application witch QGestures?
Re: Gestures and QGraphicsScene
I have the same problem. I can get QGestureEvents from the Viewport but not from a QGraphicsObject. The just do not seem to be there. Anyone know why? :confused:
Here is a example of what I am trying.
Code:
class GestureTest : QGraphicsObject {
Q_OBJECT
public:
GestureTest() {
grabGesture(Qt::PinchGesture);
}
bool sceneEvent
(QEvent* pEvent
) {
if (pEvent
->type
() == QEvent::Gesture) { std::cout << "Gesture Received!" << std::endl;
}
return QGraphicsObject::sceneEvent(pEvent);
}
};
Re: Gestures and QGraphicsScene
I would assume gesture events might not be propagated to items. It's still a new api in Qt, it might not be complete.
Re: Gestures and QGraphicsScene
Quote:
I would assume gesture events might not be propagated to items.
That could be the case. However, QGraphicsObject has a grabGesture() function. Therefore, there must be a way to make the gesture events propagate. Unless the planned API is not fully implemented.
Re: Gestures and QGraphicsScene
Quote:
Originally Posted by
stretchtiberius
Unless the planned API is not fully implemented.
That might be the case. Maybe you should try some Qt 4.7 snapshot.
Re: Gestures and QGraphicsScene
Sorry for disturbing, but I tried gesture recognition without success as well: is gesture classes only supported by some platforms or are all platforms supported?
Re: Gestures and QGraphicsScene
Quote:
Originally Posted by
wysota
That might be the case. Maybe you should try some Qt 4.7 snapshot.
The docs in 4.6 seem to suggest gestures work for graphic view's objects too.
Re: Gestures and QGraphicsScene
Quote:
is gesture classes only supported by some platforms or are all platforms supported?
From what I understand, multi-touch gestures are only supported on some platforms such as Windows 7.
Quote:
The docs in 4.6 seem to suggest gestures work for graphic view's objects too.
That is what confuses me. I am using Qt 4.6.3 and yet I get no QGestureEvents from my QGraphicsObject.
Here is a full QGraphicsObject to test gestures with. Take a look and see if I am missing something. If someone else could test test it themselves that would be great! I just added it to a standard QGraphicsScene. I am compiling 4.7 beta now and will give that a try.
Code:
#ifndef ___TOUCHOBJECT___
#define ___TOUCHOBJECT___
#include <QtGui>
#include <iostream>
class TouchObject : public QGraphicsObject {
Q_OBJECT
public:
TouchObject
(QGraphicsItem* pParent
= NULL) : QGraphicsObject
(pParent
) { grabGesture(Qt::PinchGesture);
grabGesture(Qt::PanGesture);
grabGesture(Qt::SwipeGesture);
}
return QRectF(-WIDTH
/ 2,
-HEIGHT
/ 2, WIDTH, HEIGHT
);
}
path.addEllipse(boundingRect());
return path;
}
pOption;
pWidget;
painter->setBrush(Qt::blue);
painter->drawEllipse(boundingRect());
}
bool sceneEvent
(QEvent* pEvent
) { std::cout << "Gesture Event Received from sceneEvent()" << std::endl;
}
return QGraphicsObject::sceneEvent(pEvent);
}
switch (pEvent->type()) {
std::cout << "Gesture Event Received from event()" << std::endl;
}
return QGraphicsObject::event(pEvent);
}
private:
static const int WIDTH = 80;
static const int HEIGHT = 80;
};
#endif
Re: Gestures and QGraphicsScene
Pan and swipe gestures don't require multi-touch.
Re: Gestures and QGraphicsScene
I just tested using QGraphicObject with Qt 4.7.0 beta 1. Still did not work. I am beginning to think that this might be a complete oversight by the Qt developers. Maybe passing gesture events to a QGraphicsObject is unknowingly unfinished.
Re: Gestures and QGraphicsScene
Do gesture-related examples bundled with Qt work for you?
Re: Gestures and QGraphicsScene
Quote:
Do gesture-related examples bundled with Qt work for you?
Yes the gesture example and the multi-touch examples work. However, there is no example that has a QGraphicsObject receive gestures. I think this is a major oversight.
Re: Gestures and QGraphicsScene
After playing with it for a little longer I made some interesting observations. If I do not call setAcceptTouchEvents(true) none of the QGestureRecognizers receive any QTouchEvents. If I call setAcceptTouchEvents(true) from my QGraphicsObject, the appropriate QGestureRecognizer class receives events of type QEvent::TouchBegin. However, it never receives any TouchUpdate or TouchEnd events. Because most of the recognition is in TouchUpdate none of the gestures are recognized and therefore, no QGestureEvents are created. I have no idea how to get around this problem.
Thanks for you help.
Re: Gestures and QGraphicsScene
After getting some help by sending a message to the qt4-preview-feedback@trolltech.com, I learned that you have to call setAcceptTouchEvents(true) and in the sceneEvent() acccept TouchStartedEvents and call grabGesture on the viewport as well as within the QGraphicObject. With all this you still cannot receive QPanEvents (likely because the hotspot is not defined in QPanGestureRecognizer). Here is my previous example with the necessary changes.
Within the GraphicsView
Code:
viewport()->grabGesture(Qt::PinchGesture);
TouchObject.h
Code:
#ifndef ___TOUCHOBJECT___
#define ___TOUCHOBJECT___
#include <QtGui>
#include <QGesture>
#include <iostream>
class TouchObject : public QGraphicsObject {
Q_OBJECT
public:
TouchObject
(QGraphicsItem* pParent
= NULL) : QGraphicsObject
(pParent
) {
grabGesture(Qt::PinchGesture);
setAcceptTouchEvents(true);
}
return QRectF(-WIDTH
/ 2,
-HEIGHT
/ 2, WIDTH, HEIGHT
);
}
path.addEllipse(boundingRect());
return path;
}
pOption;
pWidget;
painter->setBrush(Qt::blue);
painter->drawEllipse(boundingRect());
}
bool sceneEvent
(QEvent* pEvent
) { switch (pEvent->type()) {
pEvent->accept();
return true;
std::cout << "Gesture Event Received from sceneEvent()";
QGestureEvent* pGestureEvent = static_cast<QGestureEvent*>(pEvent);
if(QGesture *pinch = pGestureEvent->gesture(Qt::PinchGesture)) {
std::cout << " Pinch Gesture" << std::endl;
}
else {
std::cout << " not a pinch gesture." << std::endl;
}
break;
}
return QGraphicsObject::sceneEvent(pEvent);
}
private:
static const int WIDTH = 160;
static const int HEIGHT = 160;
};
#endif
I hope they fix these problems in Qt 4.7 and the documentation improves. Thank you for your help.
Re: Gestures and QGraphicsScene
Did you try this example with Qt 4.7.1? This still does not seem to work for me:
- My QGraphicsView also grabs the Gestures in question
- in my QGraphicsObject subclasss I grab the same Gestures
- in my QGraphicsObject subclass I do call setAcceptTouchEvents(true)
- my boundingRect() method returns some ridiculous large rectangle (for testing)
- I do get the QEvent::TouchBegin and I accept this event as suggested, but...
- I still never get the desired QEvent::Gesture in QGraphicsObject#sceneEvent(QEvent *) method
However I do get Gesture events in my QGraphicsScene as expected. Unfortunately it seems that Pan events are "swallowed" when the mouse hovers over any item in the scene, see http://bugreports.qt.nokia.com/browse/QTBUG-16281
That's why my idea was to create an "invisible" (note: I do not call setVisible(false) on my QGraphicsObject item - in fact in my attempt I actually draw some rectangle, to make really sure the object is in the scene!) QGraphicsObject based "Gesture Controller", which would act as an event filter on all items added to the scene.
That is on a MacBook Pro running OS X 10.6.5. As I said, I do get Gesture events inside my QGraphicsScene (off course I disabled that code while testing my QGraphicsObject approach), so Gestures are definitelly supported on my platform.
Can anyone confirm whether you have a running example using Qt 4.7.1 and receive Gestures inside a QGraphicsObject based item in a QGraphicsScene?
Thanks, Oliver
Re: Gestures and QGraphicsScene
Yes, I am using something similar in my code and it is still working with Qt 4.7.1. I'll double check this example tomorrow after work and let you know if I made any changes.
Re: Gestures and QGraphicsScene
I confirmed that the code I posted previously (the TouchObject class) still receives gesture events with Qt 4.7.1 under Windows 7. I cannot test for OS X. However, I will attach the other little code pieces I use to get it running. I hope it helps. Let me know if I can do anything else for you.
main.cpp
Code:
#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsScene>
#include "TouchObject.h"
int main(int argc, char* argv[]){
pScene->setSceneRect(-300, -300, 600, 600);
pScene->addItem(new TouchObject());
view.show();
return app.exec();
}
.pro file
Code:
TEMPLATE = app
CONFIG += qt warn_on no_keywords embed_manifest_exe
QT +=
TARGET = GraphicsObjectGestures
SOURCES = main.cpp
HEADERS = TouchObject.h
LIBS +=
CONFIG += console
# Treat warnings as errors
win32:QMAKE_CXXFLAGS += /WX
TouchObject.h is the same as before.
Re: Gestures and QGraphicsScene
Quote:
Originally Posted by
stretchtiberius
I confirmed that the code I posted previously (the TouchObject class) still receives gesture events with Qt 4.7.1 under Windows 7. I cannot test for OS X. However, I will attach the other little code pieces I use to get it running. I hope it helps. Let me know if I can do anything else for you.
...
Thanks, I will stich your example together and try it out on Mac OS X and post my results here.
By the way - on a slightly different topic, "Gesture Pan Events swallowed" - does the example work for you on Windows 7: http://bugreports.qt.nokia.com/secur...iewGesture.zip
It seems that when the mouse cursor is placed over an item the "Pan" gesture gets "swallowed", only the gesture "Begin" event is delivered to the QGraphicsScene. Pinch events always work, regardless of where the mouse is placed.
Note that in the example you would only get debug output, and the mouse cursor changes to a cross in case of Pan events. And the cursor remains a cross when the mouse cursor was placed over an item (because no "End" Gesture event is received).
If that example worked for you on Windows 7 then I am facing a Mac OS X Cocoa specific issue (on a MacBook Pro).
Thanks!
Oliver