QML 鼠标区域:以编程方式将鼠标移动到鼠标区域后,onExited 不会触发



此问题发生在Windows上,但不发生在Linux上。我没有尝试过任何其他平台。

我有一个自定义类(下面的代码(,它使用QCursor来设置鼠标位置。

问题在于以下代码(回购(:

import QtQuick 2.15
import QtQuick.Window 2.15
// Custom C++ class, implementation below
import io.github.myProject.utilities.mousehelper 1.0
Window {
visible: true
width: 800
height: 600
MouseHelper { id: mouseHelper }
MouseArea {
id: mouseArea
hoverEnabled: true
anchors.fill: parent
property var p
onPressed: {
p = mouseArea.mapToGlobal(
mouseArea.width * 0.5, mouseArea.height * 0.5);
mouseHelper.setCursorPosition(0, 0);
}
onReleased: {
mouseHelper.setCursorPosition(p.x, p.y);
}
onExited: {
console.log('This should happen twice, but it only happens once.');
}
}
}

重现问题的步骤:

  1. 将鼠标放在窗口上。光标将移动到屏幕的左上角,onExited将启动
  2. 松开鼠标按钮。光标将跳到窗口的中间
  3. 将鼠标移出窗口

onExited应该在用户将鼠标移出窗口时再次触发,但它没有。有什么办法我可以吗

  1. 导致其着火,或
  2. 否则检测到鼠标已移出鼠标区域

onPositionChanged仍然会激发,但我只能用它来检测鼠标何时靠近MouseArea的边缘,而不是何时离开。

我尝试在顶部覆盖一个全局MouseArea,并传递所有事件,作为进行手动特殊情况位置检查的一种方式,但我无法传递悬停事件。


设置鼠标位置的类:

#ifndef MOUSEHELPER_H
#define MOUSEHELPER_H
#include <QObject>
#include <QCursor>
class MouseHelper : public QObject {
Q_OBJECT
public:
explicit MouseHelper(QObject *parent = nullptr);
Q_INVOKABLE void setCursorPosition(int x, int y);
signals:
public slots:
};
#endif // MOUSEHELPER_H
#include "mousehelper.h"
#include <QGuiApplication>
MouseHelper::MouseHelper(QObject *parent) : QObject(parent) {}
void MouseHelper::setCursorPosition(int x, int y) {
QCursor::setPos(x, y);
}

我在我的主函数中将此类注册为QML类型

int main(int argc, char *argv[]) {
// ...
qmlRegisterType<MouseHelper>("io.github.myProject.utilities.mousehelper",
1, 0, "MouseHelper");
}

然后我可以将它导入QML并使用它。

作为解决问题的方法,您可以使用计时器重置鼠标光标的位置。

QML:

MouseArea {
...
Timer {
id: timer
interval: 10
repeat: false
onTriggered: {
mouseHelper.setCursorPosition(mouseArea.p.x, mouseArea.p.y)
}
}

onReleased: {
timer.start()
}
...
}

或者在MouseHelper类中:

#include <QTimer>
...
void MouseHelper::setCursorPosition(int x, int y) {
QTimer::singleShot(10, this, [x, y]() { QCursor::setPos(x, y); });
}

如果计时器的间隔不太小,这对我来说是有效的。

最新更新