Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QGestureEvent for mouse

Tags:

c++

qt

I write desktop application that works with map, and I want to react on pan and long press events. It is possible to use QGestureEvent on Qt/Linux/X11 with ordinary mouse?

I took Qt gesture example, it works on tablet, but not reaction on press left mouse button and move (I expect that application recognizes it as tap or swipe event).

Then I added to Qt gesture example app.setAttribute(Qt::AA_SynthesizeTouchForUnhandledMouseEvents, true); at main and such code to imagewidget.cpp:

void ImageWidget::mousePressEvent(QMouseEvent *e)
{
    e->ignore();
}
void ImageWidget::mouseReleaseEvent(QMouseEvent *e)
{
    e->ignore();
}
void ImageWidget::mouseMoveEvent(QMouseEvent *e)
{
    e->ignore();
}

this code still works on tablet, but again no reaction on mouse on Linux/X11.

Any way to enable qgesture on linux/x11, should I write my own gesture recognition for mouse?

like image 373
user1244932 Avatar asked Mar 14 '18 20:03

user1244932


1 Answers

The official way to make gestures out of mouse events in Qt is deriving from the QGestureRecognizer class, which allows to listen to relevant mouse events, set gesture properties accordingly, then trigger the gesture (or cancel it).

Here follows an example for pan gestures only, just to give an idea of what has to be done.

Have a QGestureRecognizer subclass like this:

#include <QGestureRecognizer>
#include <QPointF>
class PanGestureRecognizer : public QGestureRecognizer
{
  QPointF startpoint;
  bool panning;
public:
  PanGestureRecognizer() : panning(false){}
  QGesture *create(QObject *target);
  Result recognize(QGesture *state, QObject *watched, QEvent *event);
};

The create method has been overridden to return a new instance of our gesture of interest:

QGesture *PanGestureRecognizer::create(QObject *target)
{
  return  new QPanGesture();
}

The recognize method override is the core of our recognizer class, where events are passed in, gesture properties set, gesture events triggered:

QGestureRecognizer::Result PanGestureRecognizer::recognize(QGesture *state, QObject *, QEvent *event)
{
  QMouseEvent * mouse = dynamic_cast<QMouseEvent*>(event);
  if(mouse != 0)
  {
    if(mouse->type() == QMouseEvent::MouseButtonPress)
    {
      QPanGesture * gesture = dynamic_cast<QPanGesture*>(state);
      if(gesture != 0)
      {
        panning = true;
        startpoint = mouse->pos();
        gesture->setLastOffset(QPointF());
        gesture->setOffset(QPointF());
        return TriggerGesture;
      }
    }
    if(panning && (mouse->type() == QMouseEvent::MouseMove))
    {
      QPanGesture * gesture = dynamic_cast<QPanGesture*>(state);
      if(gesture != 0)
      {
        gesture->setLastOffset(gesture->offset());
        gesture->setOffset(mouse->pos() - startpoint);
        return TriggerGesture;
      }
    }
    if(mouse->type() == QMouseEvent::MouseButtonRelease)
    {
      QPanGesture * gesture = dynamic_cast<QPanGesture*>(state);
      if(gesture != 0)
      {
        QPointF endpoint = mouse->pos();
        if(startpoint == endpoint)
        {
          return CancelGesture;
        }
        panning = false;
        gesture->setLastOffset(gesture->offset());
        gesture->setOffset(mouse->pos() - startpoint);
        return FinishGesture;
      }
    }
    if(mouse->type() == QMouseEvent::MouseButtonDblClick)
    {
      panning = false;
      return CancelGesture;
    }
    return Ignore;
  }
}

Basically, we track mouse events, updating a couple of properties of our own (panning and startpoint) and the passed in gesture properties as well. For each mouse event type, we also return a QGestureRecognizer::Result . All other events are discarded (the method returns Ignore).

This code can be tested with the Image Gestures Example, though: just add the class to the project and this line in the ImageWidget constructor:

QGestureRecognizer::registerRecognizer(new PanGestureRecognizer());

This should let the user grab the picture and move it around, using a mouse.

like image 63
p-a-o-l-o Avatar answered Nov 01 '22 09:11

p-a-o-l-o