Congratulations to our 2022 Qt Champions!

[Solved again] Post a QGestureEvent to a QStateMachine

  • Hello everybody,

    i have a problem when posting QGestureEvents to a QStateMachine.
    I implemented custom gesture recognizers, which a correctly registered and now i am receiving QGestureEvents in my application. I then do some processing and afterwards i would like to post one gesture event to my state machine.

    QList<QGesture *> newGestureList;
    if (mp_stateMachine){
    	QGestureEvent *ev = new QGestureEvent(newGestureList);

    So far so good. For Gestures with the state Qt::GestureStarted and Qt::GestureUpdated everything works fine.

    Now the problem:
    When the gesture has the state Qt::GestureFinished or Qt::GestureCancelled, it gets resetted by Qt, so that in custom transition, i receive the events, but the gestures have the state Qt::NoGesture. That is very critical to me since i need to check the state afterwards.

    I found out, that in QGestureManager.cpp the cancelled and finished gestures are resetted:

        // reset gestures that ended
        QSet<QGesture *> endedGestures =
                finishedGestures + canceledGestures + undeliveredGestures + maybeToCanceledGestures;
        foreach (QGesture *gesture, endedGestures) {

    But I don't know why and how to solve my problem.

    Any ideas are welcome!

    Thank you in advance,

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Shouldn't you rather decouple the two ? Handle the gesture in your widget, then send a custom event to your state machine so the machine doesn't need to know QGestureEvent and thus you can also change easily what triggers that custom event.

    Hope it helps

  • @SGaist Thank you for your fast answer.

    In fact, I implemented custom gestures, which contain all important information for the decision.
    My custom Transition class called GestureTransition triggers according to the specified gesture event.

    Could I implement a custom event holding a QGesture object? If yes, how?

    Thanks, Malte

    Edit: I think i got it, subclassing QEvent was not that hard ;) Thank you for the idea!

  • Okay... i have to reopen this issue.

    like @SGaist proposed, I want to post a custom event to my state machine. However, I want this event to hold an instance of the causing QGesture object. Sadly, i see no way of copying such an object, and if I use a pointer, it is the same object that gets killed by Qt in the QGestureManager as said above..

    Any ideas to solve this? I have like 8 custom gesture classes that hold different information, which are all subclasses of QGesture (for the information of the QGestureState). And I don't want to miss that..

    PS: QGesture(const QGesture &) constructor does not work. It does not copy the state and properties...

  • Lifetime Qt Champion

    QGesture is a QObject thus it can't be copied.

    I'd make these information separated objects (struct or class) and use them with your new events. This will avoid passing QGesture objects around.

  • @SGaist So basically duplicating all the functionality provided by QGesture because i can't copy the objects? That doesn't sound very convenient to me.

  • Lifetime Qt Champion

    We might be misunderstanding each other. Is it correct that you have some custom data inside your custom gestures ?
    If so, what I'm suggesting is to make these custom data easily accessible and copy them to the events you send to the state machine.

  • Yes that is correct. Every of the 8 custom gestures defines its own set of data in addition to the QGestureState, which is set by the according gesture recognizer.

    I now implemented an abstract superclass for all these gestures with a method getGestureData(). This method basically wraps all the data in a QMap<QString,QVariant> to make a general gesture treatment possible. The custom event GestureEvent now takes gesture type, the Qt::GestureState and the mentioned gesture data map.
    It was a little bit circumstantial, but now it works.

    Thanks again,