问题
I would like to propagate event from a QML signal handler to C++ procedure but don't know how to pass the "event object". Take a look at this situation and pay particular attention to SomeType
.
First I create a custom QML item with a slot that can be called from QML:
class Tool : public QQuickItem
{
. . .
public slots:
virtual void onMousePositionChanged(SomeType *event);
}
Then, in QML, I create a MouseArea
and an instance of my custom object to which I wanto to propagate events to.
MouseArea {
hoverEnabled: true
onPositionChanged: {
var event = . . .
tool.onMousePositionChanged(event);
}
}
Tool {
id: tool
}
And there is the problem. I don't know how to assemble an instance of SomeType
to pass to the method. Actually I don't know what should I choose as SomeType
. Ideally this would be QQuickMouseEvent
so i could just call tool.onMousePositionChanged(mouse)
, but for some reason this type is not available for use in C++ code.
I've also considered QMouseEvent
so I would just have to rewrite the properties, but this class in turn seems to be unavailable to QML code.
I'm new to Qt Quick so maybe I'm misusing it altogether. What I want to achieve is to have this base Tool
class that has those virtual event handlers for mouse and keyboard and then create a collection of different tools that override them as necessary. They are to be displayed in some kind of toolbox, i.e. a grid with icons, from which the user can choose the current tool. The chosen tool is the one that receives the events. Any design suggestions are welcome.
回答1:
Try QEvent
, then use event->type()
for filtering the event type.
来源:https://stackoverflow.com/questions/23282488/propagate-qml-events-to-c