[Interest] Track global mouse position in QML

René Hansen renehh at gmail.com
Thu Mar 21 14:36:06 CET 2019


For now I'm doing this:

class MouseTrackableWindow : public QQuickWindow
{
  Q_OBJECT
public:
  void mouseMoveEvent(QMouseEvent *event) override {
    QQuickWindow::mouseMoveEvent(event);
    emit mouseMove(event->windowPos());
  }

signals:
  void mouseMove(QPointF mouse);
};

...

qmlRegisterType<MouseTrackableWindow>("foo.components", 1, 0,
"MouseTrackableWindow");

...

import QtQuick 2.12

import foo.components 1.0

MouseTrackableWindow {
  title: qsTr("Hello World")

  onMouseMove: {
    console.log(mouse.x, mouse.y);
  }
}

And it's not all too bad, but if anyone can tell me of a more dogmatic
QtQuick based approach, please let me know.

I'm not sure missing whatever is left in QQuickWindowQmlImpl
<https://github.com/qt/qtdeclarative/blob/5.12/src/quick/items/qquickwindowmodule_p.h>
is
going to wind up giving me problems later. Subclassing that means including
private headers etc. so I'm not to keen on going that route.


/René

On Thu, 21 Mar 2019 at 12:50 René Hansen <renehh at gmail.com> wrote:

> Hi all,
>
>
> I want to track mouse movement within my entire application window,
> because I need to show/hide/move items around where my cursor is at certain
> times. (Think e.g. custom cursor)
>
> I can do it easily by filling the entire window with a *MouseArea* and
> handle *onPositionChanged*. The problem is that any mouse sensitive
> inputs underneath, then get's blocked by the overlaying *MouseArea*. E.g.
> a *TextField* that is usually highlighted on hover and clickable, no
> longer receives any mouse events.
>
> Is there any straightforward solution to this?
>
> I know *QQuickWindow* has a *mouseMoveEvent*, but that doesn't seem to be
> exposed in QML and if I can avoid subclassing and exposing a custom class,
> I'd rather do that.
>
>
> Cheers,
>
> René Hansen
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190321/78ca7610/attachment.html>


More information about the Interest mailing list