Reputation: 9701
The C++ API has QEvent
along with multiple other classes derived from it (QMouseEvent
, QGestureEvent
etc.). QML on the other hand has events too. However I am struggling to find an elegant way of directly processing C++ events in QML.
Usually what I do is I create a custom QQuickWidget
(or similar including QQmlEngine
), override the QWidget::event(QEvent* event)
and upon receiving a specific C++ event I propagate it through signals to QML slots with the QML code being loaded through that widget. This seems like a lot of work and I'm wondering if there is some sort of QML built-in event handling for events that come from C++ context.
In particular I'm interested in handling QGestureEvent
s in QML but I guess what works for this type of events should also work for any other type of event.
Upvotes: 2
Views: 1464
Reputation: 5207
If QGestureEvent
can be copy-constructed you could simply create a Q_GADGET
based adapter:
class QmlGestureEvent : public QGestureEvent
{
Q_GADGET
Q_PROPERTY(...) // for the things you want to access from QML
public:
QmlGestureEvent(const QGestureEvent &other) : QGestureEvent(other) {}
};
If it is not copy-constructable you'll have to add data members to the adapter and copy the values from the event.
Upvotes: 1
Reputation: 49329
There is no direct support for event handling in QML, even keyboard and mouse are accessible through auxiliary objects.
QEvent
itself is not a QObject
derived, and as such, the same applies to all the derived events as well. That means no meta-information and no easy way to use from QML.
Since you are interested in a particular type of events, it would be easiest to create your own auxiliary object for those type of events, implement it in C++ and interface it to QML via signals you can attach handlers to.
Upvotes: 1