Page 2 of 2 FirstFirst 12
Results 21 to 22 of 22

Thread: Gestures and QGraphicsScene

  1. #21
    Join Date
    Feb 2009
    Posts
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Wink Re: Gestures and QGraphicsScene

    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
    That code works under Windows 7 if one bug is fixed. Change the gestureEvent() function to match the following code.

    Qt Code:
    1. bool GestureGraphicsScene::gestureEvent(const QGestureEvent *event)
    2. {
    3. bool result = false;
    4. if (QGesture *pan = event->gesture(Qt::PanGesture)) {
    5. result = panTriggered(static_cast<QPanGesture *>(pan));
    6. }
    7. if (QGesture *pinch = event->gesture(Qt::PinchGesture)) {
    8. result = pinchTriggered(static_cast<QPinchGesture *>(pinch));
    9. }
    10.  
    11. return result;
    12. }
    To copy to clipboard, switch view to plain text mode 

    For an explanation, take a look at this: http://doc.qt.nokia.com/4.7/gestures...andling-events.

    From the documentation:
    As one target object can subscribe to more than one gesture type, the QGestureEvent can contain more than one QGesture, indicating several possible gestures are active at the same time. It is then up to the widget to determine how to handle those multiple gestures and choose if some should be canceled in favor of others.

  2. #22
    Join Date
    Jan 2011
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Gestures and QGraphicsScene

    Quote Originally Posted by Oliver Knoll View Post
    Thanks, I will stich your example together and try it out on Mac OS X and post my results here.
    Unfortunatelly it really seems that on Mac (MacBook Pro with touchpad, Mac OS 10.6.6) the QGraphicsObject based items do not receive Gesture events

    I took your example and adapted it slightly:

    - use qDebug instead of stdout, so I actually see the debug statements also in Qt Creator
    - explicitly also grab gestures for the viewport! You mentioned that in your posts, but I don't see that in your actual example code (probably it is there in your real code, as you say "Within the GraphicsView", and in the example code there is no derived class of QGraphicsView)

    Anyway, just for the record:

    TouchObject.h:
    Qt Code:
    1. #ifndef ___TOUCHOBJECT___
    2. #define ___TOUCHOBJECT___
    3.  
    4. #include <QtGui>
    5. #include <QGesture>
    6. #include <iostream>
    7.  
    8. class TouchObject : public QGraphicsObject {
    9. Q_OBJECT
    10. public:
    11. TouchObject(QGraphicsItem* pParent = NULL) : QGraphicsObject(pParent) {
    12.  
    13. grabGesture(Qt::PinchGesture);
    14. setAcceptTouchEvents(true);
    15. }
    16.  
    17. QRectF boundingRect() const {
    18. return QRectF(-WIDTH / 2, -HEIGHT / 2, WIDTH, HEIGHT);
    19. }
    20.  
    21. QPainterPath shape() const {
    22. path.addEllipse(boundingRect());
    23. return path;
    24. }
    25.  
    26. void paint(QPainter *painter, const QStyleOptionGraphicsItem *pOption, QWidget *pWidget) {
    27. Q_UNUSED(pOption)
    28. Q_UNUSED(pWidget)
    29. painter->setBrush(Qt::blue);
    30. painter->drawEllipse(boundingRect());
    31. }
    32.  
    33.  
    34. bool sceneEvent(QEvent* pEvent) {
    35. QGestureEvent* pGestureEvent ;
    36. switch (pEvent->type()) {
    37. case QEvent::TouchBegin:
    38. pEvent->accept();
    39. qDebug("Touch Event Received from sceneEvent()");
    40. return true;
    41. case QEvent::Gesture:
    42. qDebug("Gesture Event Received from sceneEvent()");
    43. pGestureEvent = static_cast<QGestureEvent*>(pEvent);
    44. if(QGesture *pinch = pGestureEvent->gesture(Qt::PinchGesture)) {
    45. qDebug(" Pinch Gesture: state: %d", pinch->state());
    46. }
    47. else {
    48. qDebug(" not a pinch gesture.");
    49. }
    50. break;
    51. default:
    52. break;
    53.  
    54. }
    55.  
    56. return QGraphicsObject::sceneEvent(pEvent);
    57. }
    58.  
    59. private:
    60. static const int WIDTH = 160;
    61. static const int HEIGHT = 160;
    62. };
    63.  
    64. #endif
    To copy to clipboard, switch view to plain text mode 

    main.cpp:
    Qt Code:
    1. #include <QApplication>
    2. #include <QGraphicsView>
    3. #include <QGraphicsScene>
    4. #include "TouchObject.h"
    5.  
    6. int main(int argc, char* argv[]){
    7.  
    8. QApplication app(argc, argv);
    9. QGraphicsScene* pScene = new QGraphicsScene();
    10. pScene->setSceneRect(-300, -300, 600, 600);
    11. pScene->addItem(new TouchObject());
    12. QGraphicsView view(pScene);
    13. view.viewport()->grabGesture(Qt::PinchGesture);
    14. view.show();
    15.  
    16. return app.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 

    GraphicsObjectGesture.pro:
    Qt Code:
    1. TEMPLATE = app
    2. CONFIG += qt warn_on no_keywords
    3. QT +=
    4. TARGET = GraphicsObjectGestures
    5. SOURCES = main.cpp
    6. HEADERS = TouchObject.h
    7. LIBS +=
    8. CONFIG += console
    9.  
    10. # Treat warnings as errors
    11. win32:QMAKE_CXXFLAGS += /WX
    To copy to clipboard, switch view to plain text mode 

    The only event I really receive is the QEvent::TouchBegin (so yes, the QGraphicsObject seems to receive touch events) - but never a QEvent::Gesture event, as it seems...

    I will open a Qt issue for that and post my own example code there as well...

    Cheers, Oliver


    Added after 24 minutes:


    I created an issue for that: http://bugreports.qt.nokia.com/browse/QTBUG-16618
    Last edited by Oliver Knoll; 13th January 2011 at 08:52.

Similar Threads

  1. Qt gestures on PC
    By pingwinek in forum Qt Programming
    Replies: 10
    Last Post: 1st December 2010, 02:30
  2. Image Gestures example on OSX 10.5.8
    By jhndnn in forum Qt Programming
    Replies: 2
    Last Post: 17th September 2010, 12:55
  3. Grabing Touch Gestures on GraphicsView with Qt 4.6
    By serge in forum Qt Programming
    Replies: 0
    Last Post: 9th March 2010, 23:50
  4. Gestures
    By emrares in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 20th December 2009, 09:30
  5. in QGraphicsScene matrix of other QGraphicsScene
    By Noxxik in forum Qt Programming
    Replies: 5
    Last Post: 15th February 2009, 17:27

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.