将QML事件传播到C ++ [英] Propagate QML events to C++

查看:932
本文介绍了将QML事件传播到C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将事件从QML信号处理程序传播到C ++过程,但不知道如何传递事件对象。看看这种情况,并特别注意 SomeType

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.

首先,我创建一个自定义QML项目可以从QML调用的插槽:

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);
}


$ b 和我自定义对象的实例,我将wanto传播到的事件。

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
}

还有问题。我不知道如何组装一个 SomeType 的实例传递给方法。实际上,我不知道应该选择 SomeType 。理想情况下,这将是 QQuickMouseEvent 所以我可以调用 tool.onMousePositionChanged(mouse),但由于某种原因,不能用于C ++代码。

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.

我也考虑过 QMouseEvent ,所以我只需要重写属性,但这个类反过来似乎不可用于QML代码。

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.

我是Qt Quick的新手,所以也许我一起滥用它。我想要实现的是具有这些基本的工具类,它具有鼠标和键盘的那些虚拟事件处理程序,然后创建一个不同的工具的集合,在必要时覆盖它们。它们将被显示在某种工具箱中,即具有图标的网格,用户可以从中选择当前工具。所选择的工具是接收事件的工具。欢迎任何设计建议。

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.

推荐答案

尝试 QEvent code> event-> type()用于过滤事件类型

Try QEvent, then use event->type() for filtering the event type.

这篇关于将QML事件传播到C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆