如何正确重写qquickwindow中的mousePressEvent?

时间:2019-06-27 13:36:03

标签: c++ qml qt5

我有一个从qquickwindow继承的对象,该对象具有重写的mousePressEvent方法。

.h

class FWindow : public QQuickWindow
{
    Q_OBJECT

public:
    FWindow(QQuickWindow* parent = Q_NULLPTR);

protected:
    virtual void mousePressEvent(QMouseEvent* event) override;
};

.cpp

void FWindow::mousePressEvent(QMouseEvent* event)
{
    if (event->button() == Qt::LeftButton)
    {
        ...
    }
    QQuickWindow::mousePressEvent(event);
}

问题是,当我将带有Rectangle的{​​{1}}添加到qml文件时,它没有任何反应。信号到达MouseArea,而不是FWindow。如何解决?

.qml

MouseArea

1 个答案:

答案 0 :(得分:2)

documentation说默认情况下接受QMouseEvent* event中收到的QQuickItem::mousePressEvent,如果您不想接受,则必须致电event->ignore()