How do I pass QML events to C++ code? I want to manage these events by passing them to a VTK interactor.
Asked
Active
Viewed 564 times
1 Answers
0
In QML code:
// some shape
Rectangle {
signal signalEvent333
onYourEvent: {
signalEvent333();
}
And in C++ code:
auto* qqView = new QQuickView(); // don't forget to delete sometimes
qqView->setSource(QUrl(QStringLiteral("qrc:///res/qml/myQmlForm.qml"))); // if that qml form in the app resource
QQuickItem* root = qqView->rootObject();
connect((QObject*)root, SIGNAL(signalEvent333()), this, SLOT(onSignalEvent333()));
We assume we have onSignalEvent333 slot for 'this' object.
P.S. And this question should be marked Qt as well. And I don't know what VTK is.

Alexander V
- 8,351
- 4
- 38
- 47
-
Thanks for your response. Can I pass parameter, QMouseEvent *event, to onSignalEvent333()? – Cartik Sharma Dec 01 '14 at 17:54
-
You are signaling from QML and there is obviously a lack of native Qt types there but you can definitely try to pass something, say, integer or string. – Alexander V Dec 01 '14 at 19:00
-
I tried passing a "mouse" event which gets interpreted as a QObject *event in a C++ function. Now I'm trying to cast the event to QMouseEvent so I can process it but I get a segmentation fault when I do a const QEvent::type t = event->type(); If I can pass the event and extract it's type and other properties, it would be a step towards passing these to VTK another API. Any comments are welcome. – Cartik Sharma Dec 01 '14 at 23:46
-
try to do qDebug() << event and see what it prints out. You can only use what's in there and luckily it should have that meta information as long as it is QObject-based. – Alexander V Dec 02 '14 at 02:12
-
qDebug gives me QQuickMouseEvent followed by the address. When I cast it to QMouseEvent *, qDebug gives me QEvent. Yet, when I try to get the event type, it seg faults, perhaps because QQuickMouseEvent is implicitly private. – Cartik Sharma Dec 02 '14 at 18:00
-
Don't do an incorrect cast. Maybe explore qobject_cast but mainly out of curiosity. qobject_cast may help if there is such instance there. Somewhat relative topic: http://stackoverflow.com/questions/23282488/propagate-qml-events-to-c – Alexander V Dec 02 '14 at 19:30
-
I tried qobject_cast but it is not allowed to cast a QEvent* type. The post you sent a link to unfortunately does not result in a valid solution. In fact the error with passing QEvent * is Unknown method parameter type:QEvent*. Therefore the workaround with QObject*, which works until the point you try to access the event's function calls. – Cartik Sharma Dec 02 '14 at 20:39
-
Just signal with your own data. – Alexander V Dec 02 '14 at 20:42
-
This worked with adding the eventFilter function. I can qml events on my own with the eventFilter override. – Cartik Sharma Dec 05 '14 at 20:17