简体   繁体   中英

Propagate QML events to C++

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, ie 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.

尝试使用QEvent ,然后使用event->type()过滤事件类型

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM