Solved Position mouse cursor
-
Is it possible to move the mouse position?
Something like setPos(x, y) -
Hi,
Out of curiosity, what is your purpose for that ?
-
The designer of the app wants the mouse pointer hidden when moving a control (knob or slider) and when the mouse is released the cursor should re appear at the stop position of the control. Even if it has been moved outside of the control.
-
You can use the QCursor for that. You can take a look at this stack overflow answer to get a starting point.
-
@SGaist thanks, I got it working, kind off
as in the post you linked I need to figure out the x,y coordinates to jump to :-) -
when I have a 'complete' solution I'll post the code here, in the case somebody needs to do the same
-
Solved it \o/
Here's a (bit) shortened version of my code.
- there's some qml code to hide the cursor when moving the slider, I think I'm gonna move that to c++ too.
- according to the docs
mapToGlobal(,x,y)
returns a qmlpoint
a "x,y"
I logged the output ofmapToGlobal()
and it gave :QPointF(951, 519)
QtCursor::setPos()
can't handle theQPointF
, onlyQPoint
, so I took the x and y of that point and usedQtCursor::setPos(x,y)
.
Comments welcome :-)
C++ part
///backend.h #ifndef BACKEND_H #define BACKEND_H #include <QObject> #include <QCursor> class BackEnd : public QObject { Q_OBJECT public: explicit BackEnd(QObject *parent = nullptr); signals: public slots: void moveCursor(QPointF p); private: QCursor cursor; }; #endif // BACKEND_H
// backend.cpp #include "backend.h" BackEnd::BackEnd(QObject *parent) : QObject(parent) { } void BackEnd::moveCursor(QPointF p) { int x = p.x(); int y = p.y(); cursor.setPos(x,y); }
main.cpp #include <QGuiApplication> #include <QQmlApplicationEngine> #include "backend.h" int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); qmlRegisterType<BackEnd>("borealis.backend", 1, 0, "BackEnd"); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; return app.exec(); }
QML part
// main.qml import QtQuick 2.2 import QtQuick.Window 2.2 import borealis.backend 1.0 import "components" // Slider.qml is in dir ./components ApplicationWindow { id: borealis width: 640 height: 480 visible: true property alias backend: backend title: qsTr("Borealis") color: "#3b3b3b" BackEnd { id: backend // this links to the cpp code } Slider{ anchors.horizontalCenter: parent.horizontalCenter anchors.verticalCenter: parent.verticalCenter } }
// Slider.qml , custom slider (not using qtquick controls) Item { id: root property real value: 0.5 property int minimumValue: 0 property int maximumValue: 1 implicitWidth: 20 implicitHeight: 100 property variant mappedcursor Rectangle { id: back height: root.height width: root.width color: "slate grey" } Rectangle { id: slider height: root.height * value width: root.width anchors.bottom: back.bottom color: "dark grey" anchors.bottomMargin: 0 } Rectangle { id: handle height: 2 anchors.top: slider.top anchors.topMargin: 0 width: root.width } MouseArea { anchors.fill: root cursorShape: pressed ? Qt.BlankCursor : Qt.ArrowCursor onReleased: move_Cursor() onMouseYChanged: { var pos = maximumValue - mouse.y / root.height * (maximumValue - minimumValue) + minimumValue root.value = Math.max(minimumValue, Math.min(pos, maximumValue)) } } function move_Cursor() { mappedcursor = mapToGlobal(handle.width / 2,handle.y) backend.moveCursor(mappedcursor) } }
-
turns out I don't need to QCursor object, QCursor::setPos(x,y) works too
-
This post is deleted!