我有一个从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);
}
问题是,当我将带有MouseArea
的Rectangle
添加到 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()
。