I have a qml Window with an Item which has
Keys.onPressed {
}
And I have a c++ class which has
protected:
void keyPressEvent(QKeyEvent *event);
What needs to go inside the Keys.onPressed? I have tried
myclass.keyPressEvent(event)
and I have tried a public Q_INVOKABLE function (handleKeyPress) in my c++ class with parameter (QKeyEvent * event) from which I wanted to call the keyPressEvent.
At runtime the former gives
"TypeError: Property 'keyPressEvent' of object myclass is not a function"
The latter
"Error: Unknown method parameter type: QKeyEvent*".
(I have then found out that qml can only handle pointers to QObject, and QKeyEvent doesn't inherit from QObject but that doesn't help me in finding a solution.)
So what is the correct way to get the keyPressEvent called from qml? Or if this is the wrong approach altogether, what is the correct one if my class needs the information QKeyEvent can give me like which key was pressed etc.?
KeyEventis not aQKeyEvent *, but a class that inherits fromQObjectand encapsulates aQKeyEvent, soQKeyEventcan not be accessed, but you could access the q-properties as shown below:C++
QML:
But that is not recommended. Your class should not know the event directly, but the best thing is that you handle the logic in QML and that the function:
Or if you want to know only the key could be as follows
C++
QML