如何在qquickwindow中正确覆盖mousePressEvent?



我有一个从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);
}

问题是,当我将带有MouseAreaRectangle添加到 qml 文件时,它不会以任何方式做出反应。信号发给FWindow,而不是MouseArea。如何解决?

.qml

FWindow
{
visible: true;
Rectangle
{
width: 50;
height: 50;
color: "green";
anchors.verticalCenter: parent.verticalCenter;
anchors.horizontalCenter: parent.horizontalCenter;
MouseArea
{
anchors.fill: parent;
onClicked:
{
console.log("clicked");
}
}
}
}

文档说默认接受QQuickItem::mousePressEvent中收到的QMouseEvent* event,如果不想接受,则必须调用event->ignore()

相关内容

  • 没有找到相关文章