Propagate QML events to C++

柔情痞子 提交于 2019-12-23 13:29:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!