[Interest] Track global mouse position in QML

Jérôme Godbout godboutj at amotus.ca
Thu Mar 21 15:05:31 CET 2019


Why not use a MouseArea or the new HoverHandler. Anyway, fill the root item with a mouseare or you can use the Item function:
https://doc.qt.io/qt-5/qml-qtquick-item.html#mapToGlobal-method
to convert local mouse coordinate to global coordintate (reverse from is also available).

You can wrap this into a component that you instanciate into you root item, this will act like a optional addon and no need to subclass.


[36E56279]
une compagnie  [cid:image002.jpg at 01D4DFCD.9DD2E1C0]
RAPPROCHEZ LA DISTANCE

Jérôme Godbout
Développeur Logiciel Sénior /
Senior Software Developer

p: +1 (418) 800-1073 ext.:109

amotus.ca<http://www.amotus-solutions.com/>
statum-iot.com<http://statum-iot.com/>
[cid:image003.png at 01D4DFCD.9DD2E1C0]<https://www.facebook.com/LesSolutionsAmotus/> [cid:image004.png at 01D4DFCD.9DD2E1C0] <https://www.linkedin.com/company/amotus-solutions/>  [cid:image005.png at 01D4DFCD.9DD2E1C0] <https://twitter.com/AmotusSolutions>  [cid:image006.jpg at 01D4DFCD.9DD2E1C0] <https://www.youtube.com/channel/UCoYpQgsmj1iJZyDjTQ3x8Ig>





From: Interest <interest-bounces at qt-project.org> On Behalf Of René Hansen
Sent: March 21, 2019 9:36 AM
To: interest <interest at qt-project.org>
Subject: Re: [Interest] Track global mouse position in QML

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<mailto: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/7b606d44/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.jpg
Type: image/jpeg
Size: 6751 bytes
Desc: image001.jpg
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190321/7b606d44/attachment.jpg>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image002.jpg
Type: image/jpeg
Size: 1016 bytes
Desc: image002.jpg
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190321/7b606d44/attachment-0001.jpg>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image003.png
Type: image/png
Size: 483 bytes
Desc: image003.png
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190321/7b606d44/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image004.png
Type: image/png
Size: 506 bytes
Desc: image004.png
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190321/7b606d44/attachment-0001.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image005.png
Type: image/png
Size: 500 bytes
Desc: image005.png
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190321/7b606d44/attachment-0002.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image006.jpg
Type: image/jpeg
Size: 713 bytes
Desc: image006.jpg
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190321/7b606d44/attachment-0002.jpg>


More information about the Interest mailing list