gpt4 book ai didi

c++ - 如何在 qquickwindow 中正确覆盖 mousePressEvent?

转载 作者:行者123 更新时间:2023-11-30 03:15:45 27 4
gpt4 key购买 nike

我有一个继承自 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");
}
}
}
}

最佳答案

documentation表示默认接受 QQuickItem::mousePressEvent 中接收到的 QMouseEvent* 事件,如果您不想接受它,则必须调用 event->ignore ()

关于c++ - 如何在 qquickwindow 中正确覆盖 mousePressEvent?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56792279/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com